Using Array and let

@IBOutlet var nameHere: UITextField!


@IBOutlet var sumHere: UILabel!



@IBAction func submit(_ sender: Any) {


let (a, b, c, d, e, f, g, h, i) = (1, 2, 3, 4, 5, 6, 7, 8, 9)

let emptyArray: [Int] = []

var sum = 0

for number in emptyArray {

sum += number

}

this works when I enter items in the array manually or in playground, but when I try and build project I cant get values to appear in sumHere. Is there a problem with sumHere being a textfield? All I am trying to do is enter text in nameHere and output sum in SumHere. thanks for any advice.

Accepted Reply

OK, now it is clear.


And we need to take into account repeated letters (here "n")


So, this code does the computation.


Test it, and if it works, don't forget to mark this answer as correct to close the thread.


let dict = ["k":1, "b":2, "n":3, "d":4, "e":5, "f":6, "g":7, "h":8, "y":9]
var sum = 0
var allValues = [Int]()
for letter in nameHere {
    let value = dict[String(letter)] ?? 0
    sum += value
    allValues.append(value)
}
print("Sum =", sum, "with values", allValues)


the output is:

Sum = 21 with values [1, 5, 3, 3, 9]


You can also build the dictionary by zipping 2 arrays and get a formulation closer to your initial intent with tuples (which cannot be used here in the way tou initially intended):


let symbols =  ["k", "b", "n", "d", "e", "f", "g", "h", "y"]
let symbolsValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let dict = Dictionary(uniqueKeysWithValues: zip(symbols, symbolsValues))


You get a dict as the one defined on line 01 above. Remember that order in a dictionary is meaningless:

dict = ["b": 2, "n": 3, "e": 5, "f": 6, "g": 7, "k": 1, "d": 4, "h": 8, "y": 9]

Replies

Where do you wrote to the label ?

Your empty array is an empty array, so sum will be 0.


Line 7 is useless. Should be replaced by the filling of the array.

you have to add line 14 to write to the UILabel.


  @IBOutlet var nameHere: UITextField!

    @IBOutlet var sumHere: UILabel!

    @IBAction func submit(_ sender: Any) {

        // USELESS let (a, b, c, d, e, f, g, h, i) = (1, 2, 3, 4, 5, 6, 7, 8, 9)
        var theyArray: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]     // Now you have elements in the array
        var sum = 0
        for number in emptyArray {
            sum += number
        }

     sumHere.text = String(sum)          // ADD THIS LINE
}


Note: to compute the sum of items of the array, use reduce:


let sum = theyArray.reduce(0, +)

Thanks so much for your help. I will revamp my code. the reason I had the let commands equial a number is... each letter will equal a specific number. So when I use a text field like nameHere, it needs to break the letters into an array and calculate the letters sum. Hope that makes sense. I purchased an xcode book several years back and Udemy course last year. I not good but enjoy playing with code. thanks again for the help

OK, then you can do this :

let (a, b, c, d, e, f, g, h, i) = (1, 2, 3, 4, 5, 6, 7, 8, 9)
var theArray: [Int] = [a, b, c, d, e, f, g, h, i]

I had some typos in the code :


  @IBOutlet var nameHere: UITextField!

    @IBOutlet var sumHere: UILabel!

    @IBAction func submit(_ sender: Any) {

        // USELESS let (a, b, c, d, e, f, g, h, i) = (1, 2, 3, 4, 5, 6, 7, 8, 9)
        var theArray: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]     // Now you have elements in the array
        var sum = 0
        for number in theArray {          // theArray of course
            sum += number
        }

     sumHere.text = String(sum)          // ADD THIS LINE
}


Don't forget to close the thread by marking correct answer if that solves. Good continuation.

Hi Claude31... I really appreciate your assistance in trying to help me and you have helped. I know it is not you. I have not explained this well enough. If you can give it one more look I would appreciate it. Line 95 needs to autopopulate from what I enter in on line 92.


//@IBOutlet var nameHere: UITextField!


//@IBOutlet var sumHere: UILabel!


//@IBAction func submit(_ sender: Any) {



91 let (k, b, n, d, e, f, g, h, y) = (1, 2, 3, 4, 5, 6, 7, 8, 9) //working perferctly

92 var nameHere = "abcd" //working perferctly

93 let characters = Array(nameHere) //working perferctly


95 let array: [Int] = [h,y] // here is where my problem is. I need this Int array to auto populate from line 92 like line 93 does. This provides me with the total of what is input on line 92. This works if I enter manually like h,y = 17 on line 95.


97 var sum = array.reduce(0, +) //working perferctly

98 for number in array { //working perferctly

99 sum += number //working perferctly

100 }


//sumHere.text = String(sum) //working perferctly

Want to be sure I understand.


This works, right ?

let (k, b, n, d, e, f, g, h, y) = (1, 2, 3, 4, 5, 6, 7, 8, 9)
var nameHere = "abcd" 
let characters = Array(nameHere) 

let array: [Int] = [h,y]          // array is [8, 9]


So what do you want ? put all the items of the tuple in the array ?

something like

let tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)

let array2 = Array(tuple)

But that does not work of course.


I've not found a clean solution.


The best I could get is :

let tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
let array2 = [tuple.0, tuple.1, tuple.2, tuple.3, tuple.4, tuple.5, tuple.6, tuple.7, tuple.8]


One could think to iterate teuple.i, but that does not work.

See discussions here if you want to dig in:

h ttps://stackoverflow.com/questions/41852812/swift-access-tuple-elements-using-variables-arguments

h ttps://stackoverflow.com/questions/24746397/how-can-i-convert-an-array-to-a-tuple

Lines 1-4 work correctly. Line 2 will be where I enter my text, which will be a name. Line 3 actually breaks each letter of the name into an array (nameHere). I assume at this point nameHere is a stringed array. Line 5 however is an array that is a Int and I want the text entered from line 2 to auto populate the corresponding numbers, so I can get a total sum of the letters entered into line2.

Thanks for the links to tuples. I will read up a bit more on tuples and see if that will work for me. I appreciate your guidance and help. thank you so much. My God bless you in some way today.


let (k, b, n, d, e, f, g, h, y) = (1, 2, 3, 4, 5, 6, 7, 8, 9)

  • var nameHere = "abcd"
  • let characters = Array(nameHere)
  • let array: [Int] = [h,y] // array is [8, 9]

I want the text entered from line 2 to auto populate the corresponding numbers, so I can get a total sum of the letters entered into line2.

Could you show exactly what you would like to see in array ?

What are the numbers you mention ?


In fact, using tuples as (1, 2, 3, 4, 5, 6, 7, 8, 9) cause complexity.


You could also define directly the arrays :


let array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

or, if you want to associate a letter, use dictionary:

let dict = ["k":1, "b":2, "n":3, "d":4, "e":5, "f":6, "g":7, "h":8, "y":9]

To get the total:

var sum = 0
for value in dict.values {
    sum += value
}
print(sum)


You get

45


to get the total of characters in characters that are in nameHere

var sum = 0
for (key, value) in dict where nameHere.contains(key) {
    sum += value
}
print(sum)


For nameHere = "abcd", you get

6

Sorry for not explaining very well.

I have a tuple that assignes a number to each letter of the alphabet. let (a,b,c,etc...) = (1,2,3,etc...)

I have a UITextField called nameHere. When I enter alphabet characters in nameHere and press submit, I want to get two outputs.

The first output is converting nameHere to an array creating individual characters.

The second output is getting a sum of alphabet characters that were input in nameHere.


//@IBOutlet var nameHere: UITextField!


//@IBOutlet var sumHere: UILabel!


//@IBAction func submit(_ sender: Any) {



L91 let (k, b, n, d, e, f, g, h, y) = (1, 2, 3, 4, 5, 6, 7, 8, 9) //working perferctly

L92 var nameHere = "abcd" //working perferctly

L93 let characters = Array(nameHere) //working perferctly


L95 let array: [Int] = [h,y] // here is where my problem is. I need this Int array to auto populate from line 92 like line 93 does. This provides me with the total of what is input on line 92. This works if I enter manually like h,y = 17 on line 95.


L97 var sum = array.reduce(0, +) //working perferctly

L98 for number in array { //working perferctly

L99 sum += number //working perferctly

L100 }


L102 //sumHere.text = String(sum) //working perferctly

I feel you do not read what I posted. Did you try the code I sent ? Why doen't it give you the result you want ?

Tell what is expected result for the sum with nameHere = "abcd"


The first output is converting nameHere to an array creating individual characters.

OK, you did it

The second output is getting a sum of alphabet characters that were input in nameHere.

That is exactly what I propose you. Why isn't it OK ?


You are going with tuple. Why do you need them ?

What do you do with (k, b, n, d, e, f, g, h, y) ? I do not see them used anywhere.


And there is no convenient way to convert this tuple into an array, so look for another way. That is what I proposed.

Hi Claude, I am sorry you feel I am not reading your message, but I am. You keep telling me to input data into my init array. I do not want to enter anything into it as I want it to populate automatically from nameHere. This is the only part I am missing and I have been reading about tuples, dictionaries, etc... and try to convert the string array to an init array for auto population. I will be looking at what you sent a couple of days ago. Thanks for what help you have provided.

Can you show through an example which array you want to populate, from which content and how.


I understaood that was what you did with :

L92 var nameHere = "abcd" //working perferctly

L93 let characters = Array(nameHere) //working perferctly


you have populated the array characters with nameHere.


So, what is missing here.

What is (1, 2, 3, 4, 5, 6, 7, 8, 9) ? How is it entered (by user ? In which form ?)

What is the link with nameHere.


I just don't understand what you try to achieve. Is it a kind of Scrabble game ?


Please describe precisely the scenario, in the form :


- there is a String nameHere "abcd"; entered by user ?

- I convert to and array of chars characters


- I have another array ?

- It is entered by ?


With these 2 data, I want to compute WHAT ?

Hi Claude,

No sir, no game. My dad is a bible scholar and he did extensive research on revelations. In his research he told me God provided him with a number system to calculate the antichrist. I was able to create him an excel spreadsheet where he enters in a name and and it outputs a sum.

A little more about me. Last year I purchased a course from Udemy and was going through it. As I was working through the courses, I became curious and wanted to create this iphone app to see if I could do it; since I did it in excel. So, I started playing with let, tuples, arrays, dictionarys, sums, etc... things I learned from my courses. I have become intrigued with xcode and enjoy playing with it. Not intended to publish. Here is what I am looking for.


NameHere = kenny

Submit button to execute


Sum = 21 (1,5,3,3,9)


this is where I was using the tuple: let (k, b, n, d, e, f, g, h, y) = (1, 2, 3, 4, 5, 6, 7, 8, 9)

I was using the string array trying to convert it to a int array.

OK, now it is clear.


And we need to take into account repeated letters (here "n")


So, this code does the computation.


Test it, and if it works, don't forget to mark this answer as correct to close the thread.


let dict = ["k":1, "b":2, "n":3, "d":4, "e":5, "f":6, "g":7, "h":8, "y":9]
var sum = 0
var allValues = [Int]()
for letter in nameHere {
    let value = dict[String(letter)] ?? 0
    sum += value
    allValues.append(value)
}
print("Sum =", sum, "with values", allValues)


the output is:

Sum = 21 with values [1, 5, 3, 3, 9]


You can also build the dictionary by zipping 2 arrays and get a formulation closer to your initial intent with tuples (which cannot be used here in the way tou initially intended):


let symbols =  ["k", "b", "n", "d", "e", "f", "g", "h", "y"]
let symbolsValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let dict = Dictionary(uniqueKeysWithValues: zip(symbols, symbolsValues))


You get a dict as the one defined on line 01 above. Remember that order in a dictionary is meaningless:

dict = ["b": 2, "n": 3, "e": 5, "f": 6, "g": 7, "k": 1, "d": 4, "h": 8, "y": 9]

Hi Claude,

That solved my problem. Thank you for all your help!!!!!


I found out one thing in this. I don't know dittle about xcode.


I will pray that God will bless you today for your willingness to help.


In closing: Jesus is the answer. He is the way to eternal life. With this you and I can laugh about how ignorant I am with coding when we get to heaven.