0

I have a textfield of user input which will be a string of 2 Int's

I take them and make an array of characters.

I then want to set each of those characters to Int properties.

My code is this but it doesnt work:

    func robotStartingPositionSet() {
    let robotStart = self.robotStartPosition.text!
    let coords = Array(robotStart.characters)
    self.usersRobot.xPosition = Int(coords[0])
    self.usersRobot.yPosition = Int(coords[1])
}

Any idea how I can get the characters at those index paths to set as Int to the properties?

Thanks

2
  • Maybe you can separate your two numbers by a comma and use this: let array = string.components(separatedBy: ",") Commented Jul 30, 2017 at 16:33
  • that could work but then how do i force a comma to the user and also i still have to set the array values, so really thats no different an approach Commented Jul 30, 2017 at 16:36

1 Answer 1

3

This should work:

self.usersRobot.xPosition = Int(String(coords[0]))

Another possible solution is to separate numbers by a comma and the following code would be like this:

let coords = robotStart.components(separatedBy: ",")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.