An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction...

21
An introduction to Swift Sasha Goldshtein @goldshtn

Transcript of An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction...

Page 1: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

An introduction to Swift

Sasha Goldshtein @goldshtn

Page 2: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Page 3: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “A”+ (NSUInteger)indexOfString:(NSString *)str inArray:(NSArray *)arr { return [arr indexOfObject:str]; }

// 1) if 'str' is nil, we return NSNotFound // 2) if 'arr' is nil, we return 0

Page 4: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “B”NSMutableArray *array = [NSArray array]; [array addObject:@"hello"];

// This compiles and crashes at runtime with: // "-[__NSArrayI addObject:]: unrecognized selector // sent to instance 0x7a3141c0"

Page 5: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “C”NSMutableArray *array = [NSMutableArray array]; [array addObject:@"hello"]; [array addObject:@17]; // This compiles just fine NSString *string = array[1];

// This works and prints 17 NSLog(@"%@", string);

// But this crashes NSLog(@"%@", [string stringByAppendingString:@"hmm"]);

Page 6: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Enter: Swift

Brand new, clean, modern

Type safe, generic, functional

Compiles to native (no JIT, no GC)

Seamless interop with Objective-C

Use all existing iOS/OS X APIs

Page 7: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Page 8: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Key Language Principles

Variables vs. constants

Optional types

Pattern matching

First-class functions

Full interop with Objective-C

Page 9: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Fresh Air@interface OComplex : NSObject

@property (nonatomic) double real; @property (nonatomic) double imag;

- (instancetype)initWithReal:(double)real imag:(double)imag; - (double)magSquare; - (OComplex *)addTo:(OComplex *)other; - (OComplex *)multBy:(OComplex *)other;

@end

struct Complex { var real: Double, imag: Double func magsquare() -> Double { ... } func add(z: Complex) -> Complex { ... } func mult(z: Complex) -> Complex { ... } } func +(z1: Complex, z2: Complex) -> Complex { return z1.add(z2) } func *(z1: Complex, z2: Complex) -> Complex { return z1.mult(z2) }

Page 10: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Fresh Airfor (int x = 0; x < steps; ++x) { for (int y = 0; y < steps; ++y) { OComplex *z = [[OComplex alloc] initWithReal:0.0 imag:0.0]; int iters = 0; OComplex *c = [[OComplex alloc] initWithReal:xmin+xstep*x imag:ymin+ystep*y]; while ([z magSquare] < thresh && iters < conv) { z = [[z multBy:z] addTo:c]; ++iters; } } }

for x in 0..<steps { for y in 0..<steps { var z = Complex(real: 0, imag: 0) var iters = 0 let c = Complex(real: xmin+xstep*Double(x), imag: ymin+ystep*Double(y)) while z.magsquare() < thresh && iters < conv { z = z*z + c ++iters } } }

Page 11: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “D”let fareClass = ... var milesBonus: Double

switch fareClass { case "F", "A", "J", "C": milesBonus = 2.5 // business/first case "Y", "B": milesBonus = 2.0 // full-fare economy case "M", "H": milesBonus = 1.5 // top-tier discount economy case let fc where fc.hasPrefix("R"): milesBonus = 0 // upgrades default: milesBonus = 1.0 }

Page 12: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “E”enum HttpResponse { case Success(body: String) case Error(description: String, statusCode: Int) }

switch httpGetRequest("http://example.org") { case let .Error(desc, status) where status >= 500: println("internal server error: \(desc)") case let .Error(desc, status): println("error \(status), msg = \(desc)") case .Success(let body): println("success, body = \(body)") }

Page 13: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “F”extension Int { func repeat(task: Int -> ()) { for x in 0..<self { task(x) } } }

3.repeat { println($0) }

Page 14: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “G”

Page 15: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. […] I couldn't resist the temptation to put in a null reference, simply because it was so easy to

implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and

damage in the last forty years.”

— Sir Charles Antony Richard Hoare, 2009

Photo source: Wikipedia

Page 16: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Exhibit “H”let cell : AnyObject? = tableView. dequeueReusableCellWithIdentifier("TaskCell")

if let cell = cell as? UITableViewCell { cell.textLabel?.text = task.text cell.detailTextLabel?.text = task.dueDateString return cell }

Page 17: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Let’s Build an App!

Page 18: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Interop with Objective-C

Source: “Using Swift with Cocoa and Objective-C”, Apple

Page 19: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Swift 2.0

Error handling with try, throw, catch and defer blocks

Protocol extensions

Better interop with Objective-C (nullability annotations and generics)

Page 20: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Introduction to Swift #sddconf @goldshtn

Swift 3.0

Fully open source (on GitHub)

Linux port

Swift libraries layer, written in Swift

Page 21: An introduction to Swift - SDD Conferencesddconf.com/brands/sdd/library/Swift.pdf · Introduction to Swift #sddconf @goldshtn Enter: Swift Brand new, clean, modern Type safe, generic,

Thank You!Sasha Goldshtein

@goldshtn