Difference between revisions of "Swift Intermediate"
From Hawk Wiki
m (→Intermediate Swift) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Intermediate Swift= | =Intermediate Swift= | ||
+ | Back To [[IOS_Swift]] | ||
+ | |||
Reference | Reference | ||
[[https://developer.apple.com/videos/wwdc/2014/?id=402 WWDC Link]] | [[https://developer.apple.com/videos/wwdc/2014/?id=402 WWDC Link]] | ||
==Optional Type== | ==Optional Type== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
var optionalNumber = Int? | var optionalNumber = Int? | ||
//default init to nil | //default init to nil | ||
Line 11: | Line 13: | ||
<h4>Example</h4> | <h4>Example</h4> | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
func findIndexOfArray(needle: String, array: String[]) => Int? { | func findIndexOfArray(needle: String, array: String[]) => Int? { | ||
for (index, value) in enumerate(array) { | for (index, value) in enumerate(array) { | ||
Line 32: | Line 34: | ||
<h4>Optional Chaining</h4> | <h4>Optional Chaining</h4> | ||
Example Without Chaining: <br> | Example Without Chaining: <br> | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
var addressNumber: Int? | var addressNumber: Int? | ||
if let home = paul.residence { | if let home = paul.residence { | ||
Line 45: | Line 47: | ||
</pre> | </pre> | ||
With Chaining<br> | With Chaining<br> | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
addressNumber = paul.residence?.address?.buildingNumber?.toInt() | addressNumber = paul.residence?.address?.buildingNumber?.toInt() | ||
//addressNumber is still an optional value at this state | //addressNumber is still an optional value at this state | ||
Line 55: | Line 57: | ||
<h4>Optionals Under the Hood</h4> | <h4>Optionals Under the Hood</h4> | ||
− | <pre> | + | <pre class="brush:swift"> |
enum Optional<T> { | enum Optional<T> { | ||
case None | case None | ||
Line 63: | Line 65: | ||
==Automatic Reference Counting== | ==Automatic Reference Counting== | ||
− | + | <h4>Weak References</h4> | |
Use Weal references among objects with independent lifetimes<br> | Use Weal references among objects with independent lifetimes<br> | ||
weak references are optionals | weak references are optionals | ||
If part of the object goes away, the other objects stay | If part of the object goes away, the other objects stay | ||
− | + | <h4>Unowned References</h4> | |
Use Unowned references from owned objects with the same lifetime<br> | Use Unowned references from owned objects with the same lifetime<br> | ||
If part of the object goes away, the entire object go away | If part of the object goes away, the entire object go away | ||
==Initialization== | ==Initialization== | ||
− | Every value '''MUST''' be initialized before it is used | + | Every value '''MUST''' be initialized before it is used<br> |
+ | in Class. always set own properties before super.init()<br> | ||
+ | <pre class="brush:swift"> | ||
+ | class Car {} | ||
+ | class RaceCar: Car{ | ||
+ | var hasTurbo: Bool | ||
+ | init(turbo: Bool) { | ||
+ | hasTurbo = turbo // Do this first | ||
+ | super.init() | ||
+ | } | ||
+ | } | ||
+ | </pre> |
Latest revision as of 02:48, 18 March 2015
Contents
Intermediate Swift
Back To IOS_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])") }
Optional Chaining
Example Without Chaining:
var addressNumber: Int? if let home = paul.residence { if let postalAddress = home.Address { if let building = postalAddress.buildingNumber { if let convertedNumber = building.toInt() { addressNumber = converttedNumber } } } }
With Chaining
addressNumber = paul.residence?.address?.buildingNumber?.toInt() //addressNumber is still an optional value at this state //Combine again if let addressNumber = paul.residence?.address?.buildingNumber?.toInt() { addtoDb("paul", addressNumber) }
Optionals Under the Hood
enum Optional<T> { case None case Some(T) }
Automatic Reference Counting
Weak References
Use Weal references among objects with independent lifetimes
weak references are optionals
If part of the object goes away, the other objects stay
Unowned References
Use Unowned references from owned objects with the same lifetime
If part of the object goes away, the entire object go away
Initialization
Every value MUST be initialized before it is used
in Class. always set own properties before super.init()
class Car {} class RaceCar: Car{ var hasTurbo: Bool init(turbo: Bool) { hasTurbo = turbo // Do this first super.init() } }