## Slide 1

### Slide 1 text

Idiomatic Swift Ash Furrow @ashfurrow

No content

## Slide 3

### Slide 3 text

1.Better ways to solve familiar problems using Swift 2.Everyone is a beginner again 3.We should share what we learn

Problem-Solving

## Slide 5

### Slide 5 text

You are here You wanna be here “Problem Solving”

## Slide 6

### Slide 6 text

• It would be a shame not to take advantage of these new tools and techniques • Let’s take a look at some examples

## Slide 7

### Slide 7 text

• Completely new concept of nil • Indicates “missing” value • Replaces nil, Nil, NULL, CGRectNull, -1, NSNotFound, NSNull, etc • Haskell’s “Maybe” type • C#’s “Nullable Types” Optionals

## Slide 8

### Slide 8 text

• Works well with Swift’s compile-time type safety • Which is awesome • No, seriously, awesome • Eliminates several classes of bugs • Don’t over-use optional types Optionals

## Slide 9

### Slide 9 text

let a = someFunction() //returns Int?  if a != nil { // use a! } Optionals

## Slide 10

### Slide 10 text

let a = someFunction() //returns Int?  if let b = a {  // do something with b  }      if let a = a {  // do something with a  } Optionals

## Slide 11

### Slide 11 text

• Tuples are compound values • They are lightweight, temporary containers for multiple values • Those values can be named • Useful for functions with multiple return types Tuples

## Slide 12

### Slide 12 text

func calculate() -> (Bool, Int?) { // ... return (result, errorCode) } Tuples

## Slide 13

### Slide 13 text

func calculate() -> (Bool, Int?) { // ... return (result, errorCode) } ! let calculation = calculate() ! if (calculation.0) { // … } Tuples

## Slide 14

### Slide 14 text

func calculate() -> (Bool, Int?) { // ... return (result, errorCode) } ! let calculation = calculate() let (result, _) = calculation ! if (result) { // … } Tuples

## Slide 15

### Slide 15 text

func calculate() -> (result: Bool, errorCode: Int?) { // ... return (result: result, errorCode: errorCode) } ! let calculation = calculate() if (calculation.errorCode) { // ... } Tuples

## Slide 16

### Slide 16 text

for (key, value) in dictionary { // ... } Tuples

## Slide 17

### Slide 17 text

• New APIs shouldn’t use out parameters • eg: NSError pointers • Really great for use in pattern-matching Tuples

## Slide 18

### Slide 18 text

• Borrowed from functional programming • Really useful in tail-recursive functions • Like “switch” statements on steroids Pattern-Matching

## Slide 19

### Slide 19 text

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { switch (indexPath.section) { case 0: { switch (indexPath.row) { case 0: ...  } } break;  } } Pattern-Matching

## Slide 20

### Slide 20 text

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { switch (indexPath.section) { case ASHLoginSection: { switch (indexPath.row) { case ASHLoginSectionUserNameRow: ...  } } break;  } } Pattern-Matching

## Slide 21

### Slide 21 text

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { switch (indexPath.section, indexPath.row) { case (0, _): ... default: ... } } Pattern-Matching

## Slide 22

### Slide 22 text

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { switch (indexPath.section, indexPath.row) { case (0, let row): ... default: ... } } Pattern-Matching

## Slide 23

### Slide 23 text

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { switch (indexPath.section, indexPath.row) { case (0, let row) where row > 5: ... default: ... } } Pattern-Matching

## Slide 24

### Slide 24 text

struct IntList { var head: Int = 0 var tail: IntList? } ! ... ! switch (list.head, list.tail) { case (let head, nil): //... case (let head, let tail): //... } Pattern-Matching

## Slide 25

### Slide 25 text

• Generics are common in other languages, like C# and C++ • Using a generic type as a placeholder, we can infer the type of variables at compile- time • A part of Swift’s “safe by default” behaviour Generics

## Slide 26

### Slide 26 text

struct Stack { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } } Generics

## Slide 27

### Slide 27 text

var stack = Stack() ! var stack = Stack() ! var stack = Stack() Generics

## Slide 28

### Slide 28 text

struct Stack : Equatable { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } } ! func ==(lhs: Stack, rhs: Stack) -> Bool { return lhs.items == rhs.items } Generics

## Slide 29

### Slide 29 text

• Use stacks whenever you want to deﬁne an abstract data type structure • Whenever possible, don’t bind new data structures to existing ones • Use protocols for loose coupling Generics

## Slide 30

### Slide 30 text

• Optionals • Pattern-matching • Tuples • Generics

## Slide 31

### Slide 31 text

Everyone is a Beginner

## Slide 32

### Slide 32 text

• No one is an expert in Swift • This can be kind of stressful • Relax Everyone is a Beginner

## Slide 33

### Slide 33 text

• The beneﬁts outweighs the cost of learning • Depending on your circumstance • Have your say Everyone is a Beginner

## Slide 34

### Slide 34 text

• The hardest thing is the most important thing • Start Everyone is a Beginner

## Slide 35

### Slide 35 text

• Don’t be embarrassed to ask questions! • Try to ask in public so others can beneﬁt from the answer Everyone is a Beginner

## Slide 36

### Slide 36 text

• Let’s borrow ideas Everyone is a Beginner

## Slide 37

### Slide 37 text

• Community-based conventions and guidelines are still being established Everyone is a Beginner

## Slide 38

### Slide 38 text

We Should Share   What We Learn

## Slide 39

### Slide 39 text

• Conventions and guidelines are still in ﬂux • There’s an opportunity to signiﬁcantly alter the future of iOS and OS X programming We Should Share What We Learn

## Slide 40

### Slide 40 text

• The demand for material on Swift is HUGE • Great opportunity to get known We Should Share What We Learn

## Slide 41

### Slide 41 text

• When you teach, you learn We Should Share What We Learn

## Slide 42

### Slide 42 text

• If we all share what we learn, we all get smarter • Rising tides lift all boats We Should Share What We Learn

## Slide 43

### Slide 43 text

• Stack Overﬂow • Blogs • Tweets • Gists • Open source • Radars We Should Share What We Learn

## Slide 44

### Slide 44 text

http://github.com/artsy/eidolon

## Slide 45

### Slide 45 text

1.Better ways to solve familiar problems using Swift 2.Everyone is a beginner again 3.We should share what we learn

## Slide 46

### Slide 46 text

Let’s Make Better Mistakes Tomorrow

## Slide 47

### Slide 47 text

Thank you" @ashfurrow