0

I am trying to make an app that will allow a user to input a 9-digit integer into a text field. The app will then add the 3rd and 5th integers of the number and display the result on a separate label. I figured the best way to go about this would be to convert the 9-digit integer into an array and then adding the 2nd and 4th value of the array together and displaying the value. I am not sure how to do this though. Any help is appreciated! Thanks

1
  • Try something first. Then update your question with what you tried and explain what issues you are having. Commented Apr 11, 2016 at 20:04

1 Answer 1

1

Here's how you can do it. You take the text from text field, make an array of characters from it and then convert it to [Int] array.

let text = "24513567" // get the text from text field
let textArray = text.characters
let numbers = textArray.map { Int(String($0)) }

if let first = numbers[3], second = numbers[5] {
    let sum = first + second
}

This will yield 6 as a result

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.