iOS Development

Optional Error Handling

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

3460:489 iOS Development Fall 2014

Error Handling

#include <stdio.h>

int main() {
    printf("Hello World!\n");
    
    return 0;
}

However, Error Handling is Important

Swift Optionals

var n:Int?

Unsafe Behavior

self.detailDescriptionLabel!.text = self.detailItem!.description

Checking first

if let detail = self.detailItem {
      if let label = self.detailDescriptionLabel {
           label.text = detail.description
       }
}

Short-Circuit Evaluation

Instead of:

if i >= 0 && i <= ar.count {
    if ar[i] == 5 {
        println(i)
    }
}

we can write:

if i >= 0 && i <= ar.count && ar[i] == 5 {
    println(i)
}

Optional Chaining

self.detailDescriptionLabel?.text = self.detailItem?.description

Optional Chaining to the Rescue

self.detailDescriptionLabel?.text = self.detailItem?.description

Model Decisions