last day (27 days later) » 

8:52 PM
3
A: How do I cycle through the entire alphabet with Swift while assigning values?

Leonardo Savio Dabusimport UIKit class ViewController: UIViewController { @IBOutlet weak var strWordValue: UILabel! @IBOutlet weak var strInputField: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib....

 
@Fogmeister thank you
 
Will this also work with capital letters as well
How would I display this sum in a UILabel.
 
You need to connect a editing changed sent action to an ibaction where you can loop trough the letters and sum its values.
 
When I created the IBAction the error "Declaration is only valid at file scope" on the extension String { line. Do you want me to post the code I have as of now to see if you see any problems with it that I don't see
 
Just add a new file to your project (File > New > File > iOS Source Swift File ) call it main.swift and add those extensions there. You can also put them at the bottom of your view controller.
You are welcome
@Buster why did you unaccepted the answer?
@Buster no problem
 
8:52 PM
I dont understand this at all. I need to be able to know what this means so i can fix it if I want to change anything. This is very confusing.
 
What part you don't understand ? You just need to open your project add a new file (iOS Swift sourced file. Change the import Foundation to import UIKit and place those extensions there.
What it does it creates an array of characters for any string in this case an alphabet and try to find the letter. If found it returns the letter position which you can use as the index to extract the letter value.
Btw it is all in a single line I can't understand what part is so confusing
The only thing you may have difficult to understand is the currying I use to find out the wordValue but I think the naming I choose is self explanatory
If you post a github or your project folder zipped I can fix it for you
 
I dont understand the extensions part. What is the point of it? Why do I need it? Can't I just put in the file I already have
 
You don't I will post it as a regular function for you
Yes can put the extension bellow your view controller class in the same file if you want
 
I just don't understand what the difference is between an extension and a regular function.
We shall discuss this in a chat.
 
I have updated the answer so you can compare the extension with the regular method
whats up
the diference is that I use self instead of a parameter to do the read only computed property extension but it can be omitted thats what you might have some trouble to visualize.
 
8:57 PM
Ok. I will try this.
If I were to create an entirely new project, would this be all the code I need because that is what I did and the is an error saying: 'String' does not have a member named "wordValue". On this line: strWordValue.text = strInputField.text.wordValue.description
I am just trying to see if the code does what I want it to do. But, as said above, there is an error.
Nevermind. Fixed the error I was having.
But now there is an error on the lines like this one: let aValue = letterValue("a") saying 'ViewController' does not conform to protocol 'UnicodeScarlarLiteralConvertible'. I have no idea what this means.
 
9:20 PM
You need both functions inside the view controller or both of the at top level (global)
You should try just putting the extensions at the end of the code, same view controller file outside the view controller class brackets
 
Using them globally fixed the problem.
 
But there is still an error on the line: strWordValue.text = strInputField.text.wordValue.description with the same message.
 
Just place those extensions at top level also
 
which ones?
 
9:25 PM
Or at the and of the view controller code
Both
 
Both the function and the extension?
 
Lettervalue and word value
You only need the extension
S
 
Sorry. I think I am misunderstanding what to do.
 
"A".letterValue.description
Or
"anything".wordValue.description
 
That exact code?
just for the letter a
 
9:29 PM
Just drop those extensions at the bottom off the file
Those codes will return a string
You can assign to the text field.text =
 
So delete all the code that looks like this: let aValue = letterValue("a").
 
if you only need to display yes
Otherwise you might need it to do something else
 
The error is still there on the line: strWordValue.text = strInputField.text.wordValue.description
 
You need to add ! After strinputfield.text
I guess
If you pasted the extensions at the right place that is the only problem I can think off
 
Still there.
 
9:35 PM
Can you post a screenshot
 
Can I do that in a chat?
How?
 
you could just edit the question and add your actual code and error message
do you have dropbox
or messages
I can control your machine remotely
 
I will just edit i that is okay
 
seriouls
messages I can just see your screen and whats happening
 
I edited for you
All the errors are gone. But for some reason it won't calculate
 
9:49 PM
ok You need to connect the text field
to your code (view controller class)
right click the text field
the is SENT ACTIONS
there is
click editing changed and drag it to create the IBAction
or connect to the one you already have
you might need to delete the old ones
btw you don't need a button for this
any change to the text field will be displayed in real time
 
Ok that worked. Thanks. Just one question, should I ask a new question asking how to use a button to save the word within the textfield or is it easy enough for you to explain in this chat?
 
you want to save it to disk ?
NSUserDefaults ?
 
I am unsure of where to start.
 
you want it to load the last value if there is one?
thats easy
you can always save it when it changes
I will edit your question and include it
 
Only if the value is 100 and they click the button.
 
10:03 PM
I have edited
just saw that condition
the bottom is sumLettersAction
button
you mean 100 or more
 
No. Only when it equals 100 is the button enabled
 
i have updated
should be ok
didn't test
if you need further help let me know
 
Will a button save it or will it automatically save when it = 100? I don't want it to do it automatically.
 
only the button
 
I want the user to tap a button to save it
 
10:16 PM
sumLettersAction is a button?
if it is the text field just move the code to the new button
if strInputField.text.wordValue == 100 {
NSUserDefaults().setObject(strInputField.text, forKey: "strWordValue")
}
 
Where will it be saved to?
 
disk
you won't be able to see it
only load using NSUserDefaults
the line I've posted inside viewDidLoad
you can move it somewhere else if you don't want to load it when the view loads
strWordValue.text = NSUserDefaults().stringForKey("strWordValue") ?? "default value of your choice"
 
I would like all the saved words to be saved to a TableView when the button is clicked while also saving them so you don't lose the data within the TableView.
 
you need an array
better
you have to search it first to avoid duplicates
using if contains(yourStringArray, yourString) { ... }
better if not contains
if !contains(yourStringArray, yourString) { ... }
then you add to the array
so you can avoid the same world stored multiple times
if you need help storying arrays to NSUserDefaults I can help you also
it is pretty simple
 
10:31 PM
Ok. I think I have got it. Thanks for all your help!
 
you are welcome
 

  last day (27 days later) »