Swift after one week of coding

29
Karol Kubicki [email protected] @kar-kub Swift after one week of coding

description

© Karol Kubicki

Transcript of Swift after one week of coding

Page 1: Swift after one week of coding

Karol Kubicki [email protected]

@kar-kub

Swiftafter one week of coding

Page 2: Swift after one week of coding

- Paweł Sołyga @ Tooploox

„Pick your topic, you’ll give a talk on SwiftWro”

Page 3: Swift after one week of coding

- Karol Kubicki @ Tooploox

„Uh, sure, but I need to learn it first”

Page 4: Swift after one week of coding

Rewriting our brand new cool and trendy project

and Swift makes it even more cool and trendy

Page 5: Swift after one week of coding

My first lines in Swift

protocol SensorModuleDelegate { func sensorModuleDidUpdateWithData(sensorModule: SensorModule?, data: AnyObject) } !protocol SensorModule { var updateInterval: NSTimeInterval { get set } var delegate: SensorModuleDelegate? { get set } }

and guess what?

Page 6: Swift after one week of coding

https://devforums.apple.com/message/1023897

Circual protocol references

Page 7: Swift after one week of coding

Singleton

Objc-C+ (instancetype)sharedManager { static Manager *_sharedManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedManager = [Manager new]; }); return _sharedManager; }

Page 8: Swift after one week of coding

Singleton

Swiftprivate let _SharedInstance = Manager() !class Manager { class var sharedManager: Manager { return _SharedInstance } }

Page 9: Swift after one week of coding

Singleton Swift vs Objc-C

• Thread safe

• Swift: let

• Objc-C: dispatch_once

• Laziness

• Swift: global variables

• Objc-C: manualy

Page 10: Swift after one week of coding

Error handling

Objc-C NSError *error = nil; NSData *data = [self parseStuff:stuff error:&error]; if (error == nil) { // do stuff with data } else { // handle error }

Page 11: Swift after one week of coding

Error handling

Swiftvar response: (data: AnyObject?, error: NSError?) = parseStuff(stuff) if let errorValue = response.error { // handle error } else if let dataValue: AnyObject = response.data { // do stuff with response.data }

Page 12: Swift after one week of coding

Error handling Swift vs Objc-C

• Safety

• Swift: optional bindings makes sure that there’s value

• Objc-C: checking for nil

• Visually

• Swift: nice

• Objc-C: so 70s

Page 13: Swift after one week of coding

Laziness

Objc-C- (NSOutputStream*)outputStream { if (_outputStream == nil) { _outputStream = [[NSOutputStream alloc] initToFileAtPath:self.path append:self.append]; [_outputStream open]; } return _outputStream; }

Page 14: Swift after one week of coding

Laziness

Swiftlazy private var outFile: NSOutputStream = { var outFile = NSOutputStream(toFileAtPath: self.fileName, append: self.append) outFile.open() return outFile }()

Page 15: Swift after one week of coding

Laziness Objc-C vs Swift

• Language support

• Swift: yep

• Objc-C: getter override

• Limitation

• Objc-C: no by default

• Swift: no computed properties, no observable methods

Page 16: Swift after one week of coding

NSUserDefaults wrapperObjc-C

- (NSArray*)exportedFiles { if (_exportedFiles == nil) { _exportedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"exportedFiles"]; if (_exportedFiles == nil) { _exportedFiles = @[]; } } return _exportedFiles; } !- (void)setExportedFiles:(NSArray *)exportedFiles { _exportedFiles = exportedFiles; [[NSUserDefaults standardUserDefaults] setObject:exportedFiles forKey:@"exportedFiles"]; }

Page 17: Swift after one week of coding

NSUserDefaults wrapperSwift

private(set) var exportedFiles: [String] = { return userDefault.objectForKey("kExportedFiles") as? [String] ?? [] }() { didSet { userDefault.setObject(exportedFiles, forKey: "kExportedFiles") } }

Page 18: Swift after one week of coding

NSUserDefaults wrapper Objc-C vs Swift

• Laziness

• Objc-C: it’s lazy

• Swift: no lazy for computed properties and observable functions

• General

• Objc-C: more boilerplate code for private setters

• Swift: clean, language supported design

Page 19: Swift after one week of coding

Structs

Objc-C

DRBMotionData *newData = (DRBMotionData*)data; CMAcceleration tmp; tmp.x = (self.avgMotionData.x + newData.x) / 2; tmp.y = (self.avgMotionData.y + newData.y) / 2; tmp.z = (self.avgMotionData.z + newData.z) / 2; self.avgMotionData = tmp;

Page 20: Swift after one week of coding

Structs

Swift

if let avgMotionDataValue = avgMotionData { avgMotionData = CMAcceleration( x: (avgMotionDataValue.x + data.x) / 2, y: (avgMotionDataValue.y + data.y) / 2, z: (avgMotionDataValue.z + data.z) / 2) } else { avgMotionData = CMAcceleration(x: data.x, y: data.y, z: data.z) }

Page 21: Swift after one week of coding

Structs Objc-C vs Swift

• Objects

• Objc-C: no, can’t check if null

• Swift: no (Any), but can check for nil

Page 22: Swift after one week of coding

Curried functions

currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into

evaluating a sequence of functions, each with a single argument

http://en.wikipedia.org/wiki/Currying

Page 23: Swift after one week of coding

Curried functions

func addTwoInt(a: Int, b: Int) -> Int { return a + b } !// Playground addTwoInt(10, 20) // 30

Page 24: Swift after one week of coding

Curried functions

func add(a: Int) -> (Int -> Int) { return { (b: Int) in return a + b } } !//Playground add(10)(2) // 12 var add10 = add(10) // (Int) -> (Int) add10(5) // 15

Page 25: Swift after one week of coding

Curried functions

func addCurried(a: Int)(b: Int) -> Int { return a + b } !addCurried(10)(b: 20) // 30 var add30 = addCurried(30) // Int -> Int add30(b: 40) // 70

Page 26: Swift after one week of coding

Curried functionsclass Presentation { var slides: [String] = [] func addSlide(slide: String) { slides.append(slide) } } !var swiftWro: Presentation = Presentation() var slide_maker = Presentation.addSlide // (Presentation) -> (String) -> () slide_maker(swiftWro)("currying") swiftWro.slides

Page 27: Swift after one week of coding

Curried functions

Nice real life example !

http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/

Page 28: Swift after one week of coding

Q&A