Difference between revisions of "Swift Intermediate"
From Hawk Wiki
(Created page with "=Intermediate Swift= Reference https://developer.apple.com/videos/wwdc/2014/?id=402 WWDC Link ==Optional Type== <pre class="brush:c"> var optionalNumber = Int? //default init...") |
(No difference)
|
Revision as of 00:04, 27 January 2015
Intermediate Swift
Reference [WWDC Link]
Optional Type
var optionalNumber = Int? //default init to nil optionalNumber = 9
Non-optional type cannot be nil
Example
func findIndexOfArray(needle: String, array: String[]) => Int? { for (index, value) in enumerate(array) { if value == needle { return index } } return nil } var neighbors = ["Alex", "Anna", "Madison"] let index = findIndexOfArray("Alex", neighbors) if index { println("Found \(neighbors[index!])") // noticing the !. The ! will unwrapping the value. Must check non-nil it before unwrap. } //Optional binding: Check and unwrap at same time if let indexValue = findIndexOfArray("Alex", neighbors) { println("Found \(neighbors[indexValue])") }