Difference between revisions of "Swift Intermediate"

From Hawk Wiki
Jump to: navigation, search
(Initialization)
Line 3: Line 3:
 
[[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:c">
+
<pre class="brush:swift">
 
var optionalNumber = Int?
 
var optionalNumber = Int?
 
//default init to nil
 
//default init to nil
Line 11: Line 11:
  
 
<h4>Example</h4>
 
<h4>Example</h4>
<pre class="brush:c">
+
<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 32:
 
<h4>Optional Chaining</h4>
 
<h4>Optional Chaining</h4>
 
Example Without Chaining: <br>
 
Example Without Chaining: <br>
<pre class="brush:c">
+
<pre class="brush:swift">
 
var addressNumber: Int?
 
var addressNumber: Int?
 
if let home = paul.residence {
 
if let home = paul.residence {
Line 45: Line 45:
 
</pre>
 
</pre>
 
With Chaining<br>
 
With Chaining<br>
<pre class="brush:c">
+
<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 55:
  
 
<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 74: Line 74:
 
Every value '''MUST''' be initialized before it is used<br>
 
Every value '''MUST''' be initialized before it is used<br>
 
in Class. always set own properties before super.init()<br>
 
in Class. always set own properties before super.init()<br>
<pre>
+
<pre class="brush:swift">
 
class Car {}
 
class Car {}
 
class RaceCar: Car{
 
class RaceCar: Car{

Revision as of 00:10, 20 February 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])")
}

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()
  }
}