Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott...

277
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 228 Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer

Transcript of Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott...

Page 1: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 228

Hidden Gems in Cocoa and Cocoa Touch

Scott StevensonSoftware Engineer

Page 2: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Mattt ThompsonNSHipster.com

Page 3: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit
Page 4: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Xcode

Core Data

UIKit

AppKit

Objective-C

Foundation

Core Animation

Page 5: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Editing and debuggingXcode

Page 6: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 7: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 8: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 9: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 10: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 11: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeOpen Quickly (Command-Shift-O)

Tip

1

Page 12: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeRelated Files

Tip

2

Page 13: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

XcodeRelated Files

Tip

2

Page 14: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Breakpoint ActionsXcode

• Less time recompiling• Log message, trigger command• Conditionals

Tip

3

Page 15: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode Tip

4

Page 16: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string

Tip

4

Page 17: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription {

Tip

4

Page 18: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription { return @"A reeeeeeeaaaalllly long debug string with nerdy details.";

Tip

4

Page 19: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription { return @"A reeeeeeeaaaalllly long debug string with nerdy details.";}

Tip

4

Page 20: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription { return @"A reeeeeeeaaaalllly long debug string with nerdy details.";}

(lldb) po self.rootViewController

Tip

4

Page 21: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription { return @"A reeeeeeeaaaalllly long debug string with nerdy details.";}

(lldb) po self.rootViewControllerA reeeeeeeaaaalllly long debug string with nerdy details.

Tip

4

Page 22: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Debug descriptionXcode

• Verbose debugging string- (NSString *)debugDescription { return @"A reeeeeeeaaaalllly long debug string with nerdy details.";}

(lldb) po self.rootViewControllerA reeeeeeeaaaalllly long debug string with nerdy details.

•Use with Breakpoint Actions!

Tip

4

Page 23: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode Tip

5

Page 24: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode

<UINavigationItemView: 0x757be70; frame = (350 9; 68 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757bf60>>

Tip

5

Page 25: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode

<UINavigationItemView: 0x757be70; frame = (350 9; 68 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757bf60>><UINavigationButton: 0x7586ed0; frame = (7 7; 51 30); opaque = NO; layer = <CALayer: 0x75870b0>>

Tip

5

Page 26: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode

<UINavigationItemView: 0x757be70; frame = (350 9; 68 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757bf60>><UINavigationButton: 0x7586ed0; frame = (7 7; 51 30); opaque = NO; layer = <CALayer: 0x75870b0>> | <UIImageView: 0x7588ed0; frame = (0 0; 51 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7588f30>>

Tip

5

Page 27: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode

<UINavigationItemView: 0x757be70; frame = (350 9; 68 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757bf60>><UINavigationButton: 0x7586ed0; frame = (7 7; 51 30); opaque = NO; layer = <CALayer: 0x75870b0>> | <UIImageView: 0x7588ed0; frame = (0 0; 51 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7588f30>> | <UIButtonLabel: 0x7587c50; frame = (14 7; 23 15); text = 'Edit'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7587d40>>

Tip

5

Page 28: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Recursive descriptionXcode

<UINavigationItemView: 0x757be70; frame = (350 9; 68 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757bf60>><UINavigationButton: 0x7586ed0; frame = (7 7; 51 30); opaque = NO; layer = <CALayer: 0x75870b0>> | <UIImageView: 0x7588ed0; frame = (0 0; 51 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7588f30>> | <UIButtonLabel: 0x7587c50; frame = (14 7; 23 15); text = 'Edit'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7587d40>>

Tip

5

Page 29: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Language tipsObjective-C

Page 30: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

SubscriptingObjective-C Tip

6

Page 31: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];

Objective-C Tip

6

Page 32: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];indexedValues[0] = @"One";

Objective-C Tip

6

Page 33: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];indexedValues[0] = @"One";NSLog(@"value: %@", indexedValues[0]);

Objective-C Tip

6

Page 34: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];indexedValues[0] = @"One";NSLog(@"value: %@", indexedValues[0]);

Objective-C

NSMutableDictionary *keyedValues = [NSMutableDictionary dictionary];

Tip

6

Page 35: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];indexedValues[0] = @"One";NSLog(@"value: %@", indexedValues[0]);

Objective-C

NSMutableDictionary *keyedValues = [NSMutableDictionary dictionary];keyedValues[@"color"] = [UIColor blueColor];

Tip

6

Page 36: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Subscripting

NSMutableArray *indexedValues = [NSMutableArray array];indexedValues[0] = @"One";NSLog(@"value: %@", indexedValues[0]);

Objective-C

NSMutableDictionary *keyedValues = [NSMutableDictionary dictionary];keyedValues[@"color"] = [UIColor blueColor];NSLog(@"value: %@", keyedValues[@"color"]);

Tip

6

Page 37: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C Tip

6

Page 38: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration

Objective-C Tip

6

Page 39: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject

Objective-C Tip

6

Page 40: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;

Objective-C Tip

6

Page 41: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;

Objective-C Tip

6

Page 42: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;

Objective-C Tip

6

Page 43: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;@end

Objective-C Tip

6

Page 44: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;@end

Objective-C

• Implementation- (id)objectAtIndexedSubscript:(NSUInteger)idx { return [self.indexedValues objectAtIndex:idx];}

- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx { [self.indexedValues insertObject:obj atIndex:idx];}

Tip

6

Page 45: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscripting

•Declaration@interface RecordSet : NSObject@property (strong) NSMutableArray *indexedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;@end

Objective-C

• Implementation- (id)objectAtIndexedSubscript:(NSUInteger)idx { return [self.indexedValues objectAtIndex:idx];}

- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx { [self.indexedValues insertObject:obj atIndex:idx];}

Tip

6

Page 46: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C Tip

6

Page 47: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In use

Tip

6

Page 48: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In useRecordSet *recordSet = [[RecordSet alloc] init];

Tip

6

Page 49: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In useRecordSet *recordSet = [[RecordSet alloc] init];recordSet[0] = [Person personWithName:@"Ana"];

Tip

6

Page 50: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In useRecordSet *recordSet = [[RecordSet alloc] init];recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];

Tip

6

Page 51: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In useRecordSet *recordSet = [[RecordSet alloc] init];recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];view.person = recordSet[1];

Tip

6

Page 52: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-indexed subscriptingObjective-C

• In useRecordSet *recordSet = [[RecordSet alloc] init];recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];view.person = recordSet[1];

Tip

6

Page 53: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C Tip

6

Page 54: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration

Tip

6

Page 55: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject

Tip

6

Page 56: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;

Tip

6

Page 57: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;

Tip

6

Page 58: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

Tip

6

Page 59: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Tip

6

Page 60: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Tip

6

Page 61: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Tip

6

• Implementation- (id)objectAtKeyedSubscript:(id <NSCopying>)key { return [self.keyedValues objectForKey:key];}

- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key { [self.keyedValues setObject:obj forKey:key];}

Page 62: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Tip

6

• Implementation- (id)objectAtKeyedSubscript:(id <NSCopying>)key { return [self.keyedValues objectForKey:key];}

- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key { [self.keyedValues setObject:obj forKey:key];}

Page 63: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

•Declaration@interface Person : NSObject@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Tip

6

• Implementation- (id)objectAtKeyedSubscript:(id <NSCopying>)key { return [self.keyedValues objectForKey:key];}

- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key { [self.keyedValues setObject:obj forKey:key];}

Page 64: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C Tip

6

Page 65: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

• In use

Tip

6

Page 66: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

• In usePerson *person = [[Person alloc] init];

Tip

6

Page 67: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

• In usePerson *person = [[Person alloc] init];person[@"favoriteColor"] = [UIColor blueColor];

Tip

6

Page 68: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

• In usePerson *person = [[Person alloc] init];person[@"favoriteColor"] = [UIColor blueColor];view.backgroundColor = person[@"favoriteColor"];

Tip

6

Page 69: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Custom-keyed subscriptingObjective-C

• In usePerson *person = [[Person alloc] init];person[@"favoriteColor"] = [UIColor blueColor];view.backgroundColor = person[@"favoriteColor"];

Tip

6

Page 70: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C Tip

6

Page 71: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration

Objective-C Tip

6

Page 72: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject

Objective-C Tip

6

Page 73: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;

Objective-C Tip

6

Page 74: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;

Objective-C Tip

6

Page 75: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;

Objective-C Tip

6

Page 76: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;

Objective-C Tip

6

Page 77: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;

Objective-C Tip

6

Page 78: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

Objective-C Tip

6

Page 79: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Objective-C Tip

6

Page 80: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscripting

•Declaration@interface UltraRecordSet : NSObject@property (strong) NSMutableArray *indexedValues;@property (strong) NSMutableDictionary *keyedValues;- (id)objectAtIndexedSubscript:(NSUInteger)idx;- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;- (id)objectAtKeyedSubscript:(id <NSCopying>)key;- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;@end

Objective-C Tip

6

Page 81: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C Tip

6

Page 82: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In use

Tip

6

Page 83: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];

Tip

6

Page 84: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";

Tip

6

Page 85: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";recordSet[0] = [Person personWithName:@"Ana"];

Tip

6

Page 86: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];

Tip

6

Page 87: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];view.cityName = recordSet[@"cityName"];

Tip

6

Page 88: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];view.cityName = recordSet[@"cityName"];view.person = recordSet[1];

Tip

6

Page 89: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Combined subscriptingObjective-C

• In useUltraRecordSet *recordSet = [[UltraRecordSet alloc] init];recordSet[@"cityName"] = @"Cupertino";recordSet[0] = [Person personWithName:@"Ana"];recordSet[1] = [Person personWithName:@"Dave"];view.cityName = recordSet[@"cityName"];view.person = recordSet[1];

Tip

6

Page 90: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

• Private declarations not necessary since Xcode 4.3@interface Example (PrivateMethods)- (NSString *)privateStatusStringForKey:(NSString *)key;@end

@implementation Example- (NSString *)currentStatusString {

return [self privateStatusStringForKey:self.currentKey];}

- (NSString *)privateStatusStringForKey:(NSString *)key { self.statusStringsByKey[key];

}@end

Tip

7

Page 91: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

• Private declarations not necessary since Xcode 4.3@interface Example (PrivateMethods)- (NSString *)privateStatusStringForKey:(NSString *)key;@end

@implementation Example- (NSString *)currentStatusString {

return [self privateStatusStringForKey:self.currentKey];}

- (NSString *)privateStatusStringForKey:(NSString *)key { self.statusStringsByKey[key];

}@end

Tip

7

Page 92: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

@implementation Example- (NSString *)currentStatusString {

return [self privateStatusStringForKey:self.currentKey];}

- (NSString *)privateStatusStringForKey:(NSString *)key { self.statusStringsByKey[key];

}@end

Tip

7

Page 93: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C Tip

7

Page 94: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

• Synthesize not necessary for @property since Xcode 4.4@implementation Example@synthesize currentKey;@synthesize statusStringsByKey;

- (void)methodName { ...}

@end

Tip

7

Page 95: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

• Synthesize not necessary for @property since Xcode 4.4@implementation Example@synthesize currentKey;@synthesize statusStringsByKey;

- (void)methodName { ...}

@end

Tip

7

Page 96: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Reduce code noiseObjective-C

• Synthesize not necessary for @property since Xcode 4.4@implementation Example- (void)methodName { ...}

@end

Tip

7

Page 97: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

DemoXcode

Mattt ThompsonNSHipster.com

Page 98: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Data model tipsFoundation

Scott StevensonSoftware Engineer

Page 99: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation Tip

8

Page 100: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch

Tip

8

Page 101: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

Tip

8

Page 102: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation

Tip

8

Page 103: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count

Tip

8

Page 104: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count■ Dependencies

Tip

8

Page 105: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count■ Dependencies

•Objective-C API

Tip

8

Page 106: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count■ Dependencies

•Objective-C API■ Subclasses

Tip

8

Page 107: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count■ Dependencies

•Objective-C API■ Subclasses■ Categories

Tip

8

Page 108: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperationFoundation

• Built on Grand Central Dispatch• Features

■ Cancellation■ Max count■ Dependencies

•Objective-C API■ Subclasses■ Categories■ Key-Value Observing

Tip

8

Page 109: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

FoundationNSOperation

Tip

8

Page 110: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation Tip

8

Page 111: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

Page 112: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];

Page 113: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];

Page 114: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

Page 115: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

Page 116: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

Page 117: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;

Page 118: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;operation =[[WWDCSetFavoriteOperation alloc] initWithSuccessBlock:block];

Page 119: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;operation =[[WWDCSetFavoriteOperation alloc] initWithSuccessBlock:block];operation.favorite = status;

Page 120: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;operation =[[WWDCSetFavoriteOperation alloc] initWithSuccessBlock:block];operation.favorite = status;[self addDependenciesForAuthorizedOperation:operation];

Page 121: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;operation =[[WWDCSetFavoriteOperation alloc] initWithSuccessBlock:block];operation.favorite = status;[self addDependenciesForAuthorizedOperation:operation];[self.operationQueue addOperation:operation];

Page 122: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSOperation dependenciesFoundation

- (void)addDependenciesForAuthorizedOperation:(WWDCOperation *)operation {

Tip

8

self.authOperation = [[WWDCUserAuthorizationOperation alloc] init];[operation addDependency:self.authOperation];[self.queue addOperation: self.authOperation waitUntilFinished:NO];

}

- (void)setFavorite:(BOOL)status forSessionID:(NSString *)sessionIDcompletion:(void(^)(BOOL))block {

WWDCSetFavoriteOperation *operation;operation =[[WWDCSetFavoriteOperation alloc] initWithSuccessBlock:block];operation.favorite = status;[self addDependenciesForAuthorizedOperation:operation];[self.operationQueue addOperation:operation];

}

Page 123: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation Tip

9

Page 124: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try math

Tip

9

Page 125: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try mathNSString *text = @"3 + 5 * 4e10";

Tip

9

Page 126: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try mathNSString *text = @"3 + 5 * 4e10";NSExpression *e = [NSExpression expressionWithFormat:text, nil];

Tip

9

Page 127: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try mathNSString *text = @"3 + 5 * 4e10";NSExpression *e = [NSExpression expressionWithFormat:text, nil];NSNumber *result = [e expressionValueWithObject:nil context:nil];

Tip

9

Page 128: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try mathNSString *text = @"3 + 5 * 4e10";NSExpression *e = [NSExpression expressionWithFormat:text, nil];NSNumber *result = [e expressionValueWithObject:nil context:nil];NSLog(@"result: %@", result);

Tip

9

Page 129: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSExpressionFoundation

• Stand back, I'm going to try mathNSString *text = @"3 + 5 * 4e10";NSExpression *e = [NSExpression expressionWithFormat:text, nil];NSNumber *result = [e expressionValueWithObject:nil context:nil];NSLog(@"result: %@", result);

Tip

9

result: 200000000003

Page 130: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness, fast membership lookup•Membership cache• Set calculations

Tip

10

Page 131: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness, fast membership lookup•Membership cache• Set calculations

Tip

10

-intersectsSet: == YES

Page 132: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness, fast membership lookup•Membership cache• Set calculations

Tip

10

-intersectsSet: == YES -isSubsetOfSet: == YES

Page 133: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness•Membership cache• Set calculations

Tip

10

Page 134: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness•Membership cache• Set calculations

Tip

10

-minusSet:

Page 135: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness•Membership cache• Set calculations

Tip

10

-minusSet: -unionSet:

Page 136: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness•Membership cache• Set calculations

Tip

10

Page 137: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSSet and NSOrderedSetFoundation

•Guaranteed uniqueness•Membership cache• Set calculations

Tip

10

Designing Code for Performance Nob HillFriday 9:00AM

Page 138: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Collection tricksFoundation Tip

11

Page 139: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Collection tricksFoundation

• Reverse arrays quickly inlineNSArray *numbers = @[ @1, @2, @3 ];NSArray *reversed = numbers.reverseObjectEnumerator.allObjects;

Tip

11

Page 140: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Collection tricksFoundation

• Reverse arrays quickly inlineNSArray *numbers = @[ @1, @2, @3 ];NSArray *reversed = numbers.reverseObjectEnumerator.allObjects;

•Guarantee a mutable objectNSArray *unknown = self.values; // may be nilNSMutableArray *newArray = [NSMutableArray arrayWithArray:unknown];

Tip

11

Page 141: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Collection tricksFoundation

• Reverse arrays quickly inlineNSArray *numbers = @[ @1, @2, @3 ];NSArray *reversed = numbers.reverseObjectEnumerator.allObjects;

•Guarantee a mutable objectNSArray *unknown = self.values; // may be nilNSMutableArray *newArray = [NSMutableArray arrayWithArray:unknown];

•Declare and enumerate different collection typesid<NSFastEnumeration> collection = values;for (id object in collection) {

... }

Tip

11

Page 142: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Foundation

@implementation Manager

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len {

return [self.subordinates countByEnumeratingWithState:state objects:buffer count:len];}

@end

NSFastEnumeration to your class

Page 143: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Data model tipsFoundation and Core Foundation

Mattt ThompsonNSHipster.com

Page 144: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

• Scalars• Structs

■ Ranges■ Points, sizes, and rects

•Unretained references

Page 145: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue Tip

12

Page 146: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

NSMutableArray *array = [@[] mutableCopy];array[0] = [NSValue valueWithPoint:CGPointZero];array[1] = [NSValue valueWithRange:NSMakeRange(3, 17)];

Tip

12

Page 147: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

NSMutableArray *array = [@[] mutableCopy];array[0] = [NSValue valueWithPoint:CGPointZero];array[1] = [NSValue valueWithRange:NSMakeRange(3, 17)];

typedef struct RGB {float red, green, blue;

} _RGB;

Tip

12

Page 148: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

NSMutableArray *array = [@[] mutableCopy];array[0] = [NSValue valueWithPoint:CGPointZero];array[1] = [NSValue valueWithRange:NSMakeRange(3, 17)];

typedef struct RGB {float red, green, blue;

} _RGB;

RGB color = {1.0f, 0.0f, 0.0f};

Tip

12

Page 149: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

NSMutableArray *array = [@[] mutableCopy];array[0] = [NSValue valueWithPoint:CGPointZero];array[1] = [NSValue valueWithRange:NSMakeRange(3, 17)];

typedef struct RGB {float red, green, blue;

} _RGB;

RGB color = {1.0f, 0.0f, 0.0f};array[2] = [NSValue valueWithBytes:&color objCType:@encode(RGB)];

Tip

12

Page 150: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSValue

• object conforms to <NSCopying>NSMutableDictionary *dictionary = [@{} mutableCopy];dictionary[object] = @42;

• object does not conform to <NSCopying>NSMutableDictionary *dictionary = [@{} mutableCopy];dictionary[[NSValue valueWithNonretainedObject:object]] = @42;

Tip

12

Page 151: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding

[employee valueForKey:@"name"];employee.name;

[employee valueForKeyPath:@"manager.name"];employee.manager.name;

Tip

13

Page 152: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding Tip

13

Page 153: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding

NSArray *words = @[@"Alpha", @"Bravo", @"Charlie"];

Tip

13

Page 154: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding

NSArray *words = @[@"Alpha", @"Bravo", @"Charlie"];

[words valueForKey:@"uppercaseString"];// @[@"ALPHA", @"BRAVO", @"CHARLIE"]

Tip

13

Page 155: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding

NSArray *words = @[@"Alpha", @"Bravo", @"Charlie"];

[words valueForKey:@"uppercaseString"];// @[@"ALPHA", @"BRAVO", @"CHARLIE"]

[words valueForKey:@"length"]; // @[@5, @5, @7]

Tip

13

Page 156: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Key-Value Coding

- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; for (NSString *key in keys) { dictionary[key] = [myObject valueForKey:key]; }}

- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues; for (NSString *key in dictionary.allKeys) { id value = dictionary[key]; [myObject setValue:value forKey:key];}

Tip

13

Page 157: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

KVC Collection Operators

[employee valueForKeyPath:@"colleagues.@count"];

[staff valueForKeyPath:@"@avg.salary"];

[week valueForKeyPath:@"@max.temperature"];

Tip

14

Page 158: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

KVC Collection Operators

• Simple collection operators•Object operators•Array and set operators

Page 159: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

KVC Collection Operators

@"[email protected]"

Left Key Path Collection Operator Right Key Path

Page 160: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Simple Collection Operators

Page 161: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Simple Collection Operators

@count NSNumber

Page 162: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Simple Collection Operators

@count NSNumber

@sum NSNumber

@avg NSNumber

Page 163: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Simple Collection Operators

@count NSNumber

@sum NSNumber

@avg NSNumber

@max id

@min id

Page 164: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Object Operators

@unionOfObjects NSArray

@distinctUnionOfObjects NSArray

Page 165: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

KVC Collection Operators

•Remove duplicate values in array without NSSet[array valueForKeyPath:@"@distinctUnionOfObjects.self"]

Tip

14

Page 166: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Array and Set Operators

@unionOfArrays NSArray or NSSet

@distinctUnionOfArrays NSArray or NSSet

@distinctUnionOfSets NSArray or NSSet

Tip

14

Page 167: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Array and Set Operators Tip

14

Page 168: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Array and Set Operators

@unionOfArrays

Tip

14

Page 169: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Array and Set Operators

@distinctUnionOfArrays

Tip

14

Page 170: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

Page 171: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

Page 172: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

Page 173: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

•Dates•Addresses• Links• Phone numbers• Transit information

Page 174: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector Tip

15

Page 175: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

NSString *string = @"123 Main St. / (555) 555-5555";

Tip

15

Page 176: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

NSString *string = @"123 Main St. / (555) 555-5555";

NSError *error;

Tip

15

Page 177: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

NSString *string = @"123 Main St. / (555) 555-5555";

NSError *error;NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber error:&error];

Tip

15

Page 178: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSDataDetector

NSString *string = @"123 Main St. / (555) 555-5555";

NSError *error;NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber error:&error];

[detector enumerateMatchesInString:string options:kNilOptions range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { NSLog(@"Match: %@", result);

}];

Tip

15

Page 179: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CFStringTransform

Page 180: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CFStringTransform

• Strip accents and diacritics

Page 181: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CFStringTransform

• Strip accents and diacritics•Name Unicode characters

Page 182: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CFStringTransform

• Strip accents and diacritics•Name Unicode characters• Encode XML hex entities

Page 183: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CFStringTransform

• Strip accents and diacritics•Name Unicode characters• Encode XML hex entities• Transliterate between writing systems

Page 184: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Boolean CFStringTransform (CFMutableStringRef string,CFRange *range,CFStringRef transform,Boolean reverse

);

CFStringTransform

Page 185: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Strip Accents and Diacritics

Énġlišh låcks iñterêsţing diaçrïtičş

Page 186: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Strip Accents and Diacritics

English lacks interesting diacritics

Page 187: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSMutableString *string = [@"Énġlišh låcks iñterêsţing diaçrïtičş" mutableCopy];

CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformStripCombiningMarks, NO);

Strip Accents and Diacritics Tip

16

Page 188: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Page 189: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Character Name

A LATIN CAPITAL LETTER A

Å LATIN CAPITAL LETTER A WITH RING ABOVE

☃ SNOWMAN

Page 190: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Character Name

A LATIN CAPITAL LETTER A

Å LATIN CAPITAL LETTER A WITH RING ABOVE

☃ SNOWMAN

Page 191: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Character Name

A LATIN CAPITAL LETTER A

Å LATIN CAPITAL LETTER A WITH RING ABOVE

☃ SNOWMAN

Page 192: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Character Name

A LATIN CAPITAL LETTER A

Å LATIN CAPITAL LETTER A WITH RING ABOVE

☃ SNOWMAN

Page 193: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

Page 194: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

NSMutableString *string = [@"🐷 " mutableCopy];

CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformToUnicodeName, NO);

Tip

17

Page 195: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Name Unicode Characters

@"PIG FACE"

Tip

17

Page 196: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliterate Between Orthographies

안녕하세요

Page 197: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliterate Between Orthographies

NSMutableString *string = [@"오빤 강남스타일" mutableCopy];CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformToLatin, NO);

Tip

18

Page 198: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

annyeonghaseyo

Transliterate Between Orthographies

Page 199: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliteration | Transformations

Page 200: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliteration | Transformations

привет

γειά σου 안녕שלום

สวัสดี中文

Page 201: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliteration | Transformations

Page 202: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

mrḥbạprivet

geiá sou

annyeong

şlwm

s wạs dī

zhōng wén

Transliteration | Transformations

Page 203: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

ひらがな カタカナ

Transliteration | Transformations

Page 204: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

ヒラガナ かたかな

Transliteration | Transformations

Page 205: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

hiragana katakana

Transliteration | Transformations

Page 206: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

“On OS X v10.4 and later, with CFStringTransform you can also use

any valid ICU transform ID defined in the ICU User Guide for Transforms.”

Apple Developer Documentation

Page 207: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Transliteration | Transformations

Page 208: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

!"#$%&!#'"

Բարև Ձեզহ"ােলা

హ"gɹiːtɪŋs

नम#$

!"ોTransliteration | Transformations

Page 209: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

hello! こんにちは! สวัสดี! !"#$%! 您好!

Page 210: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input Tip

19

Page 211: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

Tip

19

Page 212: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

Tip

19

Page 213: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

Tip

19

Page 214: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

// Hello! kon'nichiha! swasdi! mrhba! nin hao!

Tip

19

Page 215: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

// Hello! kon'nichiha! swasdi! mrhba! nin hao!CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks, NO);

Tip

19

Page 216: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

// Hello! kon'nichiha! swasdi! mrhba! nin hao!CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks, NO);

// hello! kon'nichiha! swasdi! mrhba! nin hao!

Tip

19

Page 217: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);

CFMutableStringRef string; string = (__bridge CFMutableStringRef)[@"Hello! こんにちは! สวัสดี! مرحبا! 您好!" mutableCopy];

// Hello! kon'nichiha! swasdi! mrhba! nin hao!CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks, NO);

// hello! kon'nichiha! swasdi! mrhba! nin hao!CFStringLowercase(string, NULL);

Tip

19

Page 218: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input Tip

19

Page 219: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];

Tip

19

Page 220: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];[string enumerateLinguisticTagsInRange:NSMakeRange(0, string.length)

Tip

19

Page 221: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];[string enumerateLinguisticTagsInRange:NSMakeRange(0, string.length)

scheme:NSLinguisticTagSchemeTokenType

Tip

19

Page 222: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];[string enumerateLinguisticTagsInRange:NSMakeRange(0, string.length)

scheme:NSLinguisticTagSchemeTokenType options:kNilOptions

Tip

19

Page 223: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];[string enumerateLinguisticTagsInRange:NSMakeRange(0, string.length)

scheme:NSLinguisticTagSchemeTokenType options:kNilOptions orthography:nil

Tip

19

Page 224: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

NSMutableArray *mutableWords = [NSMutableArray array];[string enumerateLinguisticTagsInRange:NSMakeRange(0, string.length)

scheme:NSLinguisticTagSchemeTokenType options:kNilOptions orthography:nil usingBlock:

^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {

if ([tag isEqualToString:NSLinguisticTagWord]) { [mutableWords addObject:[string substringWithRange:tokenRange]]; }}];

Tip

19

Page 225: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

hello! こんにちは! สวัสดี! !"#$%! 您好!

Page 226: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Normalizing Input

hello! こんにちは! สวัสดี! !"#$%! 您好! hello, kon'nichiha, swasdi, mrhba, nin, hao

Page 227: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Animation tipsCore Animation

Scott StevensonSoftware Engineer

Page 228: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit
Page 229: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core Animation

UIKit

Core Animation

OpenGL ES

Core OS

AppKit

iOS OS X

Core Animation

OpenGL

Core OS

Page 230: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UIView / NSView

CALayer

Core Animation

CAAnimation @"color"

CAAnimation @"position"

Time

Page 231: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAGradientLayer

Tip

20

Page 232: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAGradientLayerCore Animation

CAGradientLayer *gradient = [CAGradientLayer layer];gradient.frame = CGRectMake(150, 250, 500, 500);

UIColor *c1 = [UIColor colorWithRed:0.09 green:0.70 blue:0.98 alpha:1.0];UIColor *c2 = [UIColor colorWithRed:0.07 green:0.41 blue:0.95 alpha:1.0];UIColor *c3 = [UIColor colorWithRed:0.81 green:0.46 blue:0.93 alpha:1.0];gradient.colors = @[(id)c2.CGColor, (id)c3.CGColor, (id)c3.CGColor];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"colors"];anim.toValue = @[(id)c1.CGColor, (id)c2.CGColor, (id)c2.CGColor];anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"colors"];

[self.view.layer addSublayer:gradient];

Tip

20

Page 233: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAGradientLayerCore Animation

CAGradientLayer *gradient = [CAGradientLayer layer];gradient.frame = CGRectMake(150, 250, 500, 500);

UIColor *c1 = [UIColor colorWithRed:0.09 green:0.70 blue:0.98 alpha:1.0];UIColor *c2 = [UIColor colorWithRed:0.07 green:0.41 blue:0.95 alpha:1.0];UIColor *c3 = [UIColor colorWithRed:0.81 green:0.46 blue:0.93 alpha:1.0];gradient.colors = @[(id)c2.CGColor, (id)c3.CGColor, (id)c3.CGColor];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"colors"];anim.toValue = @[(id)c1.CGColor, (id)c2.CGColor, (id)c2.CGColor];anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"colors"];

[self.view.layer addSublayer:gradient];

Tip

20

Page 234: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAGradientLayerCore Animation

CAGradientLayer *gradient = [CAGradientLayer layer];gradient.frame = CGRectMake(150, 250, 500, 500);

UIColor *c1 = [UIColor colorWithRed:0.09 green:0.70 blue:0.98 alpha:1.0];UIColor *c2 = [UIColor colorWithRed:0.07 green:0.41 blue:0.95 alpha:1.0];UIColor *c3 = [UIColor colorWithRed:0.81 green:0.46 blue:0.93 alpha:1.0];gradient.colors = @[(id)c2.CGColor, (id)c3.CGColor, (id)c3.CGColor];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"colors"];anim.toValue = @[(id)c1.CGColor, (id)c2.CGColor, (id)c2.CGColor];anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"colors"];

[self.view.layer addSublayer:gradient];

Tip

20

Page 235: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAGradientLayerCore Animation

CAGradientLayer *gradient = [CAGradientLayer layer];gradient.frame = CGRectMake(150, 250, 500, 500);

UIColor *c1 = [UIColor colorWithRed:0.09 green:0.70 blue:0.98 alpha:1.0];UIColor *c2 = [UIColor colorWithRed:0.07 green:0.41 blue:0.95 alpha:1.0];UIColor *c3 = [UIColor colorWithRed:0.81 green:0.46 blue:0.93 alpha:1.0];gradient.colors = @[(id)c2.CGColor, (id)c3.CGColor, (id)c3.CGColor];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"colors"];anim.toValue = @[(id)c1.CGColor, (id)c2.CGColor, (id)c2.CGColor];anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"colors"];

[self.view.layer addSublayer:gradient];

Tip

20

Page 236: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAGradientLayerCore Animation

CAGradientLayer *gradient = [CAGradientLayer layer];gradient.frame = CGRectMake(150, 250, 500, 500);

UIColor *c1 = [UIColor colorWithRed:0.09 green:0.70 blue:0.98 alpha:1.0];UIColor *c2 = [UIColor colorWithRed:0.07 green:0.41 blue:0.95 alpha:1.0];UIColor *c3 = [UIColor colorWithRed:0.81 green:0.46 blue:0.93 alpha:1.0];gradient.colors = @[(id)c2.CGColor, (id)c3.CGColor, (id)c3.CGColor];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"colors"];anim.toValue = @[(id)c1.CGColor, (id)c2.CGColor, (id)c2.CGColor];anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"colors"];

[self.view.layer addSublayer:gradient];

Tip

20

Page 237: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAGradientLayer

Tip

20

Page 238: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAShapeLayer

Tip

21

Page 239: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 240: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 241: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 242: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 243: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 244: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

CAShapeLayerCore Animation Tip

21

CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.frame = CGRectMake(150, 250, 500, 500);

UIBezierPath* path1 = [UIBezierPath bezierPath];// ... add points to shapeshapeLayer.path = path1.CGPath;

UIBezierPath* path2 = [UIBezierPath bezierPath];// ... add points to shape

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];anim.toValue = (id)path2.CGPath;anim.duration = 4.0;anim.autoreverses = YES;anim.repeatCount = 1e100;[gradient addAnimation:anim forKey:@"path"];

[self.view.layer addSublayer:gradient];

Page 245: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAShapeLayer

Tip

21

Page 246: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAShapeLayer + CAGradientLayer = ?

Tip

22

Page 247: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAShapeLayer + CAGradientLayer = ?

Tip

22

Page 248: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core AnimationCAShapeLayer + CAGradientLayer = ?

Tip

22

self.gradientLayer.mask = shapeLayer;

Page 249: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Tip

22Interactive animationCore Animation

Page 250: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Tip

22CAMediaTiming for interactive animationsCore Animation

#import "MyViewController.h"#import <QuartzCore/QuartzCore.h>

@interface MyViewController ()

@property (nonatomic, weak) IBOutlet UIView *squareContainer;@property (nonatomic, weak) IBOutlet UIView *square;@property (nonatomic, weak) IBOutlet UISlider *slider;

@end

Page 251: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Tip

22CAMediaTiming for interactive animationsCore Animation

- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.square.layer.speed = 0;

CGRect rect = self.squareContainer.bounds; rect.size.height = CGRectGetMinY(self.slider.frame); CGFloat dx = CGRectGetWidth(self.square.frame)/2.0; CGFloat dy = CGRectGetHeight(self.square.frame)/2.0; CGRect r = CGRectInset(rect, dx, dy); CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation.path = [[UIBezierPath bezierPathWithOvalInRect:r] CGPath]; animation.calculationMode = kCAAnimationPaced; animation.speed = 0.25; [self.square.layer addAnimation:animation forKey:@"position"];}

Page 252: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Tip

22CAMediaTiming for interactive animationsCore Animation

- (IBAction)sliderChanged:(id)sender { self.square.layer.timeOffset = self.slider.value;}

Page 253: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Tip

22Interactive animationCore Animation

Page 254: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Performance tipsCore Data

Page 255: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit
Page 256: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core Data

NSManagedContext

NSManagedObject

NSManagedObject

NSManagedObject

NSFetchRequest

NSManagedObject

NSManagedObject

NSManagedObject

NSPersistentStore

NSPersistentStoreCoordinator

Page 257: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

NSPrivateQueueConcurrencyTypeCore Data

Main Queue

Private Queue

-performBlock:

Tip

23

Your App

Main Context Worker Context

Page 259: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Really fast fetchesCore Data

•Only specific propertiesNSFetchRequestfetch.propertiesToFetch = @[@"name", @"phone"];

•Only the raw valuesfetch.resultType = NSDictionaryResultType

•Only the object idfetch.resultType = NSManagedObjectIDResultType

•Only the countfetch.resultType = NSCountResultType

Tip

24

Page 260: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Really fast fetchesCore Data

•Work in groups of objectsNSFetchRequestfetch.fetchBatchSize = 100;

• Reduce cross-entity fetchesfetch.relationshipKeyPathsForPrefetching = @[ @"artist", @"catalog" ];

Tip

25

Page 261: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Really fast fetchesCore Data

•Work in groups of objectsNSFetchRequestfetch.fetchBatchSize = 100;

• Reduce cross-entity fetchesfetch.relationshipKeyPathsForPrefetching = @[ @"artist", @"catalog" ];

Tip

25

Core Data Performance Optimization and Debugging Nob HillWednesday 2:00PM

Page 262: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Core Data

NSPersistentStore

NSIncrementalStore

NSIncrementalStore

Custom Store Type

Tip

26

Page 263: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Implementation tipsUIKit

Page 264: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UICollectionViewUIKit Tip

27

Page 265: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UICollectionViewUIKit Tip

27

Page 266: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UICollectionViewUIKit

•Grid views, custom layouts• Similar to UITableView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)cv { return self.collectionViewSections.count;}

- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section { return [[self sessionsInSection:section] count];}

Tip

27

Page 267: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UICollectionViewUIKit Tip

27

Page 268: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

UICollectionViewUIKit

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)path { NSArray *sessions = [self sessionsInSection:path.section]; WWDCSession* session = sessions[indexPath.item];

WWDCiPadScheduleSessionView *cell = nil; cell = [cv dequeueReusableCellWithReuseIdentifier:@"sessionView" forIndexPath:path]; [cell setSession:session]; return cell;}

Tip

27

Page 269: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Modern improvementsAppKit

Page 270: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

View-based NSTableViewAppKit Tip

28

Page 271: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

View-based NSTableViewAppKit

• Similar to UITableView•Hardware accelerated•Arbitrary views, no NSCell subclassing!• Layout in IB•Animation

NSTableViewAnimationEffectFadeNSTableViewAnimationEffectGapNSTableViewAnimationSlideUpNSTableViewAnimationSlideDownNSTableViewAnimationSlideLeftNSTableViewAnimationSlideRight

Tip

28

Page 272: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

View-based tablesAppKit

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return self.objects.count;}

- (NSTableRowView *)tableView:(NSTableView *)table rowViewForRow:(NSInteger)row { MyRowView *rowView; rowView = [table makeViewWithIdentifier:@"RowView" owner:self]; return rowView;}

Tip

29

Page 273: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

View-based tablesAppKit

- (NSView *)tableView:(NSTableView *)table viewForTableColumn:(NSTableColumn *)col row:(NSInteger)row {

NSTableCellView *cellView = nil; id objectForRow = self.objects[row]; BOOL isSelected = (row == self.selectedRow);

if ([self tableView:table isGroupRow:row]) { cellView = [table makeViewWithIdentifier:@"GroupView" owner:self]; cellView.textField.stringValue = [object title]; cellView.textField.textColor = (isSelected) ? [NSColor blueColor]; return cellView; } else { ...

Tip

29

Page 274: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

View-based tablesAppKit

if ([self tableView:table isGroupRow:row]) { cellView = [table makeViewWithIdentifier:@"GroupView" owner:self]; cellView.textField.stringValue = [object title]; cellView.textField.textColor = (isSelected) ? [NSColor blueColor]; return cellView;} else { MyGridView *gridView = [table makeViewWithIdentifier:@"Grid" owner:self]; gridView.dataSource = self.gridDataSource; gridView.delegate = self; return gridView;}

Tip

29

Page 275: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

More Information

Dave DeLongApp Frameworks and Developer Tools [email protected]

Apple Developer Documentationhttp://developer.apple.com/documentation

Apple Developer Forumshttp://devforums.apple.com

Page 276: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit

Related Sessions

Designing Code for Performance Nob HillFriday 9:00AM

Core Data Performance Optimization and Debugging Nob HillWednesday 2:00PM

Page 277: Hidden Gems in Cocoa and Cocoa Touch - Apple Inc. · Hidden Gems in Cocoa and Cocoa Touch Scott Stevenson Software Engineer. Mattt Thompson NSHipster.com. Xcode Core Data UIKit AppKit