Upload
jonathan-lott
View
166
Download
0
Embed Size (px)
Citation preview
Outline
• Strong vs. Weak variables
• Blocks & Closures (and Capture Lists in Swift)
• Private vs Public and inheritance models
• Singleton Models and memory management
• Instruments Tools
• Collection Classes (Array, Dictionary, String)
• NSObject and Swizzling Objects
Vocabulary
• “Methods”• NSArray, NSDictionary, NSString, NS-etc.• Blocks• Square brackets - Methods, NSArrays• Curly brackets - NSDictionaries• Semicolons
Objective-C
Coding Zero To Coding Hero
Write LESS Code....But How???
Abstraction Layers
KISS = Keep It Short & Specific
Coding Zero To Coding Hero
Write LESS Code....But How???
Abstraction Layers
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationDelay:1.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; self.frame = newFrame; [UIView commitAnimations];
KISS = Keep It Short & Specific
Coding Zero To Coding Hero
Write LESS Code....But How???
Abstraction Layers
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationDelay:1.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; self.frame = newFrame; [UIView commitAnimations];
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.frame = newFrame; } completion:nil];
=
KISS = Keep It Short & Specific
Coding Zero To Coding Hero
Write LESS Code....But How???
Abstraction Layers
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in playerVC.view.frame = playerFrame }, completion: nil)
=
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.frame = newFrame; } completion:nil];
KISS = Keep It Short & Specific
Blocks & ClosuresBlocks - container for code that acts like a dynamic function that can be executed at some future time or within scope of another function
Blocks & ClosuresBlocks - container for code that acts like a dynamic function that can be executed at some future time or within scope of another function
Benefits:
• Reduce code
• Reduce dependency on delegates
• More contained, readable code
Blocks & ClosuresBlocks - container for code that acts like a dynamic function that can be executed at some future time or within scope of another function
Closures - Swift implementation of Blocks (but better)
Benefits:
• Reduce code
• Reduce dependency on delegates
• More contained, readable code
Blocks & Closures [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationDelay:1.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; self.frame = newFrame; [UIView commitAnimations];
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.frame = newFrame; } completion:nil];
=
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.frame = newFrame; } completion:nil];
=
BlocksBlocks Declarations
http://goshdarnblocksyntax.com
As a local variable:returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
As a property:@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
As a method parameter:- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
As an argument to a method call:[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
As a typedef:typedef returnType (^TypeName)(parameterTypes);TypeName blockName = ^returnType(parameters) {...};
ClosuresClosure Declarations
http://goshdarnclosuresyntax.com
As a variable:var closureName: (parameterTypes) -> (returnType)
As an optional variable:var closureName: ((parameterTypes) -> (returnType))?
As a type alias:typealias closureType = (parameterTypes) -> (returnType)
As a constant:let closureName: closureType = { ... }
As an argument to a function call:func({(parameterTypes) -> (returnType) in statements})
Strong vs Weak
• When do you use strong and weak?Strong - when you don’t want it to go away
Weak - when you don’t want it holding you back
Strong vs Weak
• When do you use strong and weak?Strong - when you don’t want it to go away
Weak - when you don’t want it holding you back
• IBOutlets should be changed to strong if you plan on moving them in and out of view
Private vs Public Variables
Private - Can only be accessed by instances of MyClass
Public - Can be accessed by any object
Protected - Can be accessed by instances and subclasses of MyClass
(Protected is the default in Obj-C)http://stackoverflow.com/questions/844658/what-does-private-mean-in-objective-c
Private vs Public Variables
Private - Can only be accessed by instances of MyClass
Public - Can be accessed by any objectIn Swift
Internal - Can be accessed by any object in the module (Internal is the default in Swift)
Private vs Public Variables
Private - Can only be accessed by instances of MyClass
Public - Can be accessed by any objectIn Swift
Where is “Protected”? Read Apple’s explanation here: https://developer.apple.com/swift/blog/?id=11
Internal - Can be accessed by any object in the module (Internal is the default in Swift)
Singleton ModelsSingleton pattern - One instance at all times
http://www.raywenderlich.com/46988/ios-design-patterns
• Only One
• Single global access point
• Lazy loaded to save memory
Singleton ModelsSingleton pattern - One instance at all times
http://www.raywenderlich.com/46988/ios-design-patterns
• Only One
• Single global access point
• Lazy loaded to save memory
• Keep It Short & Specific (KISS)
Singleton ModelsApple Uses Singletons
[NSUserDefaults standardUserDefaults]
[UIApplication sharedApplication]
[UIScreen mainScreen]
[NSFileManager defaultManager]
Singleton ModelsGCD Example
+ (id)sharedManager { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; }
Singleton ModelsOld School Example
+ (id)sharedManager { static MyManager *sharedMyManager = nil; @synchronized(self) { if (sharedMyManager == nil) sharedMyManager = [[self alloc] init]; } return sharedMyManager; }
Singleton ModelsSwift Example
class MyManager { class var sharedMyManager : MyManager { struct MyManagerSingleton { static let instance = MyManager() }
return MyManagerSingleton.instance } }
NSObjectThe “Mother” of all objects.
• NSObject Protocol• isEqual• isKindOfClass (or “as? SomeClass”)• copy (requires NSCopying Protocol)• methods can be “swizzled”• objects can be extended (Categories / Extensions)
Collection ClassesKey things to know about working with
collection classes
• Enumerators• Swift Higher Order Functions
• (filter, map, reduce)• Always validate content in collection
before using
Collection ClassesSimplify all validation logic
// dict will return nil if no value for key // nil evaluates to 0 or false in Obj-C if([dict[key] isEqualToString:otherString]) { [self storeNewStringFunc:dict[key]]; }
=
if([dict objectForKey:key]) { if([[dict objectForKey:key] isEqualToString:otherString]) { [self storeNewStringFunc:[dict objectForKey:key]]; } }
TipsKey things to do to prevent coding headaches later
• When code gets “bulky” or “messy”, create new objects to “clean it up” (Remember KISS)
• Create Categories / Extensions as often as possible
• Run Instruments. Don’t run from Instruments.
• Use Blocks / Closures in place of delegates
• Comment your code as much as possible
References• https://developer.apple.com/library/ios/documentation/Swift/
Conceptual/Swift_Programming_Language/AccessControl.html
• http://www.raywenderlich.com/46988/ios-design-patterns
• http://www.galloway.me.uk/tutorials/singleton-classes/
• http://www.raywenderlich.com/79850/collection-data-structures-swift
• https://developer.apple.com/swift/blog/?id=11
• http://goshdarnclosuresyntax.com
• http://goshdarnblocksyntax.com
• http://stackoverflow.com/questions/844658/what-does-private-mean-in-objective-c
Coding Zero To Coding Hero
Jonathan Lottcontact - [email protected]
Cool Demo Source Code at https://github.com/Irving-iOS-Jumpstart/Cool-Demo-ObjC-Swift