0

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.

2
  • 1
    Is it gonna be a single textField ? you can use componentsSeparatedByString(",") Commented 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. Commented Dec 19, 2015 at 0:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

much easier to use ", " instead of ","
@LeoDabus Not necessarily. What if the user did not enter a space after each comma? It is best to assume that the user is stupid or that they will make mistakes. Trimming whitespaces and using "," will avoid this user error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.