Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I convert a String to an Int in swift

Hi I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?

4 Answers

I've seen this explained in one of the courses but I can't remember which. Something like the following should work...

var str = "100"
println(str.toInt())

str being the text() property of a UITextField.

Ive tried that before (didn't work) :(

abdulah binmadi Works for me within Storyboards and inside an app. What version of XCode are you running?

Version 6.1.1

I found a way :) Thanks jamie

Alex Hedley
Alex Hedley
16,381 Points

Which way did you find? It would help others to know how you solved it.

You take the first string var myString = "1" That's a string not an int Create another object and use the .toInt() above like this: var myStringAsAnInt = myString.toInt() You have to create another object