Post on 15-Jan-2015
description
- Paweł Sołyga @ Tooploox
„Pick your topic, you’ll give a talk on SwiftWro”
- Karol Kubicki @ Tooploox
„Uh, sure, but I need to learn it first”
Rewriting our brand new cool and trendy project
and Swift makes it even more cool and trendy
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?
https://devforums.apple.com/message/1023897
Circual protocol references
Singleton
Objc-C+ (instancetype)sharedManager { static Manager *_sharedManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedManager = [Manager new]; }); return _sharedManager; }
Singleton
Swiftprivate let _SharedInstance = Manager() !class Manager { class var sharedManager: Manager { return _SharedInstance } }
Singleton Swift vs Objc-C
• Thread safe
• Swift: let
• Objc-C: dispatch_once
• Laziness
• Swift: global variables
• Objc-C: manualy
Error handling
Objc-C NSError *error = nil; NSData *data = [self parseStuff:stuff error:&error]; if (error == nil) { // do stuff with data } else { // handle error }
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 }
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
Laziness
Objc-C- (NSOutputStream*)outputStream { if (_outputStream == nil) { _outputStream = [[NSOutputStream alloc] initToFileAtPath:self.path append:self.append]; [_outputStream open]; } return _outputStream; }
Laziness
Swiftlazy private var outFile: NSOutputStream = { var outFile = NSOutputStream(toFileAtPath: self.fileName, append: self.append) outFile.open() return outFile }()
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
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"]; }
NSUserDefaults wrapperSwift
private(set) var exportedFiles: [String] = { return userDefault.objectForKey("kExportedFiles") as? [String] ?? [] }() { didSet { userDefault.setObject(exportedFiles, forKey: "kExportedFiles") } }
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
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;
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) }
Structs Objc-C vs Swift
• Objects
• Objc-C: no, can’t check if null
• Swift: no (Any), but can check for nil
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
Curried functions
func addTwoInt(a: Int, b: Int) -> Int { return a + b } !// Playground addTwoInt(10, 20) // 30
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
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
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
Curried functions
Nice real life example !
http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/
Q&A