an introduction to swift - sdd conferencesddconf.com/brands/sdd/library/swift.pdf · introduction...
TRANSCRIPT
An introduction to Swift
Sasha Goldshtein @goldshtn
Introduction to Swift #sddconf @goldshtn
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
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"
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"]);
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
Introduction to Swift #sddconf @goldshtn
Introduction to Swift #sddconf @goldshtn
Key Language Principles
Variables vs. constants
Optional types
Pattern matching
First-class functions
Full interop with Objective-C
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) }
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 } } }
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 }
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)") }
Introduction to Swift #sddconf @goldshtn
Exhibit “F”extension Int { func repeat(task: Int -> ()) { for x in 0..<self { task(x) } } }
3.repeat { println($0) }
Introduction to Swift #sddconf @goldshtn
Exhibit “G”
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
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 }
Let’s Build an App!
Introduction to Swift #sddconf @goldshtn
Interop with Objective-C
Source: “Using Swift with Cocoa and Objective-C”, Apple
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)
Introduction to Swift #sddconf @goldshtn
Swift 3.0
Fully open source (on GitHub)
Linux port
Swift libraries layer, written in Swift
Thank You!Sasha Goldshtein
@goldshtn