NSNumber Literals• NSNumber *anInt = [NSNumber numberWithInt: 10];• NSNumber *aBool =[NSNumber numberWithBool: YES];• NSNumber *aChar = *NSNumber numberWithChar: ‘z’+;• Too much typing• We have all used NSString literals such as @”this”• NSNumber *aNum = @10;• NSNumber *aBool = @YES;• NSNumber *aChar = @’z’;
Boxed Expressions• @( <an expression> )• Eg. NSNumber *aDouble = @(2 * pi * r);• Also works with cstrings, enums
Arrays and Dictionaries• To create a NSArray or NSDictionary, we need to use factorymethods such as:• [NSArray arrayWithObjects: obj1, @"abc", obj3, nil];• [NSDictionary dictionaryWithObjectsAndKeys: obj1, obj2, key1,key2, nil];• To access an object:• [array objectAtIndex: 1];• *dictionary objectForKey: @”key”+;• To modify/update/change an object:• [array replaceObjectAtIndex: 1 withObject: @"new object"];• [dictionary setObject: @"new Object" forKey: @"key3"];
Container Literals• NSArray• @*@”one”, @”two”, @”three”+;• NSDictionary• Just like JSON• @, @”key1” : @”value1”, @”key2” : @”value2”-• No more confusing factory methods
What’s Storyboard?• A .storyboard is basically A huge XML file which containsXCode Interface Builder information• Comprised of individual “scenes” (Views + Controllers)• Transitions/Relationships between scenes are defined by“segues”
Advantages of UsingStoryboards• Clearly defined flow• Less nib file cluttering• Reduce cluttering even more when you are making anuniversal app• Using segues saves you from writing millions of IBActions withrepetitive push and pop view controller codes.• Too easy to use• Build a working UI prototype extremely fast with almost zerocoding• Storyboard knows about view-controllers, so you can createmore powerful views (eg. Static table view cells, prototypecells)
Disadvantages of UsingStoryboards• Impossible to merge storyboard changes• Solution 1: Don’t work on it at the same time• Solution 2: Based on program flow, use multiple storyboards• Not backwards compatible with iOS 4- (also iOS 5 if you areusing features such as embedded segue for container view)
How to Integrate Storyboard inYour App• Each project can have a main storyboard set in project settings• View controllers can also be loaded from storyboard filesprogrammatically:• In this way, we can use multiple storyboards with NIBs.
Segues• A segue defines a visual transition OR relationship betweentwo view controllers• Examples of transition segues:• Push segue (push view controller onto a navigation controller)• Modal segue (presenting a view controller modally)• Popover segue (Presenting a UIPopoverController, iPad only)• Custom segue (subclass UIStoryboardSegue and override the-perform method to do whatever you want. Can be reused inStoryboard)• Examples of relationship segues:• A empty navigation controller has a relationship segue to its rootview controller• A container view has a embed segue to its child view controller
Performing Segue• Define a segue by ctrl-drag from a control (such as UIButton)to another view controller• Will be triggered when the specified action is performed on thatcontrol• Define a segue by ctrl-drag from a view controller to anotherview controller• Segue needs to be triggered manually by calling–performSegueWithIdentifier:sender:
Pass Data Between Controllers• Implement prepareForSegue:(UIStoryboardSegue *)sender:(id) inthe view controller that will initiate the segue• This method is called before a segue is about to be performed• The UIStoryboardSegue parameter contains the followingproperties: identifier, sourceViewController, anddestinationViewController• Now you can set properties and send messages to thesecontrollers!
Unwind Segues• Unwind segue is a segue that jump back to previous scenes• To unwind from controller B back to controller A:1. In controller A, create a method that takes aUIStoryboardSegue as parameter with return type IBAction2. In storyboard, select controller B, ctrl-drag from a control to the“Exit” icon at the bottom of the controller3. Select the method that you defined in B. The unwind segue hasbeen created.4. On the left panel of the interface builder, select the unwindsegue and give it an identifier.5. prepareForSegue:sender: will be called in B, and then theIBAction method in A will be called
Storyboard: Conclusion• You can avoid using delegation pattern in some cases if youuse segues• With iOS 7 introduction due in June, we should start buildingnew apps with storyboards (and autolayout)
UI Customization withUIAppearance Protocol• Customize UIKit objects (eg. UIView, UIButton,…) globally• Customize specific elements depending on view hierarchy
Useful iOS Stuff
Singleton Best Practices• Restricting instantiation of a class to one object only• Example: ServiceManager• One object is then shared by many• Issue: Instantiation in a multi-threaded environment
Basic Singleton• Not thread-safe
Mutex Lock• Takes lock every time when you only needs to lock it once.Expansive.
-Initialize• If you send any message to the class, singleton object will becreated.
Double-checked Locking• Check if singleton is instantiated before and after taking lock• Memory barriers ensures all load and store CPU operationscompletes before proceeding• Doesn’t take lock every time, but still have to pay forOSMemoryBerrier()
Grand Central Dispatch• As fast as non-thread safe version• Similar to double checked locking, but avoidOSMemoryBarrier() penalty• Guaranteed run only once
Custom Container ViewControllers• Embed multiple view controllers in one view controller• Adding, removing, and transition between child viewcontrollers• Its like implementing your own UINavigationController orUITabBarController
Adding / RemovingChild View Controllers
Transition Between Child ViewControllers
Grand Central Dispatch isreally good, but I don’t havetime to finish the slide!• Efficient, fast C API• dispatch_sync, dispatch_async, dispatch_after to queue operations• dispatch_semaphore, dispatch_group, dispatch_barrier forsynchronization• You can create your own serial OR concurrent queues withdispatch_queue_create• dispatch_apply for concurrent enumeration on an array• Create “dispatch sources” such as dispatch timers that attach to aqueue and fire periodic events• Use dispatch_io to do sync/async operations on file descriptors