Convert string to integer

 Problem

I have a string and want to parse its integer value (if any).



 Recipe


// String literal
let s = "1986"

// Get Integer value from string
var n = Int(s)

// It's an Optional Int -
// in case the conversion goes wrong
print(n!)


				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground