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
abdulah binmadi
810 PointsHow 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
Jamie Barton
14,498 PointsI'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.
abdulah binmadi
810 PointsVersion 6.1.1
abdulah binmadi
810 PointsI found a way :) Thanks jamie
Alex Hedley
16,381 PointsWhich way did you find? It would help others to know how you solved it.
James Lambert
Courses Plus Student 14,477 PointsYou 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
abdulah binmadi
810 Pointsabdulah binmadi
810 PointsIve tried that before (didn't work) :(
Jamie Barton
14,498 PointsJamie Barton
14,498 Pointsabdulah binmadi Works for me within Storyboards and inside an app. What version of XCode are you running?