jwarris91
jwarris91

Reputation: 952

Setting Character from array made from string as an Int?

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

Upvotes: 0

Views: 50

Answers (1)

Phyber
Phyber

Reputation: 1368

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: ",")

Upvotes: 3

Related Questions