I'm working on making a basic grade calculator app that asks the user to enter their grades like this ( 66, 77, 88, etc). When they press the calculate button, it is going to take their input and change it into an array. How do I do that, though? I've looked around on here, apple's website, and treehouse, so I'll appreciate any help that will guide me in the right way. I do not want an exact answer, just some guidance to find the right answer.
-
1Is it gonna be a single textField ? you can use componentsSeparatedByString(",")Leo Dabus– Leo Dabus2015-12-19 00:27:32 +00:00Commented Dec 19, 2015 at 0:27
-
iTunes University has lectures from Stanford; one series by Paul Hegarty actually handles this exact concept and builds a stack-calculator as you referred to. It might be beneficial for you to review, very helpful overall.camelCase– camelCase2015-12-19 00:28:36 +00:00Commented Dec 19, 2015 at 0:28
Add a comment
|
1 Answer
You can use componentsSeparatedByString.
Example:
let input = "66, 77, 88"
// Remove any whitespace to avoid user error
let gradesString = input.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
let myStrings = gradesString.componentsSeparatedByString(",")
// Convert array of [String] to [Double]
var myGrades = [Double]()
for string in myStrings {
myGrades.append(Double(string))
}
// Use myGrades