120
Cool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2

Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

  • Upload
    buingoc

  • View
    239

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Cool Cocoa code

Corbin DunnCocoa Software Engineer

Cocoa Tips and Tricks

2

Page 2: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Introduction

• Quick and cool Cocoa tips and tricks• Learn new things you may not know• Focused mostly on AppKit classes

3

Page 3: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

What You’ll Learn

• TableView tips• Nib tips• Accessibility tips• Image tips• Document tips• PathControls, SplitViews, and CollectionViews• …and more

4

Page 4: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

DON’T PANIC

5

Page 5: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

TableView Tips

Corbin DunnCocoa Software Engineer

6

Page 6: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Links in a TableViewNSAttributedString

Custom Cell

7

Page 7: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"Title"];

[attrString addAttribute:NSLinkAttributeName value:aURL range:range];

[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSSingleUnderlineStyle] range:range];

-attributedStringValue for the NSTextFieldCell

• NSAttributedString with the NSLinkAttributeName attribute

Creating a Link

8

Page 8: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Custom Cell

LinkTextFieldCell sample code

LinkTextFieldCell in the sample code

- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {

BOOL result = [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag];

NSAttributedString *attrValue = [self attributedStringValue]; NSURL *link = [attrValue attribute:NSLinkAttributeName

atIndex:0 effectiveRange:NULL]; if (link != nil) { // We do have a link -- open it! // Okay, but how? } return result;}

9

Page 9: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Using Blocks as a Click Handler

@interface LinkTextFieldCell : NSTextFieldCell {@private void (^_linkClickedHandler)(NSURL *url, id sender);}

@property(copy) void (^linkClickedHandler)(NSURL *url, id sender);

@end

10

Page 10: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

LinkTextFieldCell sample codeCustom CellLinkTextFieldCell in the sample code

- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {

BOOL result = [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag]; NSAttributedString *attrValue = [self attributedStringValue]; NSRange effectiveRange; NSURL *link = [attrValue attribute:NSLinkAttributeName

atIndex:0 effectiveRange:&effectiveRange]; if (link != nil && _linkClickedHandler != nil) {

_linkClickedHandler(link, self); } return result;}

11

Page 11: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Ctrl-Shift Click in Interface Builder

12

Page 12: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Using a Custom Cell in IBSelection in the nib

13

Page 13: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Using Blocks as a Click Handler

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { if ([cell isKindOfClass:[LinkTextFieldCell class]]) { LinkTextFieldCell *linkCell = (LinkTextFieldCell *)cell; // Setup the work to be done when a link is clicked linkCell.linkClickedHandler = ^(NSURL *url, id sender) { [[NSWorkspace sharedWorkspace] openURL:url]; }; }}

14

Page 14: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { if ([cell isKindOfClass:[LinkTextFieldCell class]]) { LinkTextFieldCell *linkCell = (LinkTextFieldCell *)cell; // Setup the work to be done when a link is clicked linkCell.linkClickedHandler = ^(NSURL *url, id sender) { [[NSWorkspace sharedWorkspace] openURL:url]; }; }}

Using Blocks as a Click Handler

15

Page 15: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Document Tips

Mark PiccirelliCocoa Software Engineer

16

Page 16: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Overriding NSDocument Correctly

• Try to override as little as possible■ Don’t override a –save… method when you can override a–write… method

• See “Message Flow in the Document Architecture”■ See <AppKit/NSDocument.h> comments too

• We keep adding features■ The less customization you do, the more your app gets for free

17

Page 17: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Overriding NSDocument Correctly

• –write… and –save… methods are passed the URL and type• Use those instead of [self fileURL] and [self fileType]

- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError;

• Don’t set values in –write… methods

18

Page 18: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Use Blocks Even with Pre-Block API

• AppKit has a lot of methods that look like this

- (void)saveDocumentWithDelegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo;

• You have to add a method just to find out when the work is done• With blocks you can add a few generic methods and use them over and over

19

Page 19: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Use Blocks Even with Pre-Block API

• Add generic block-invoking methods like this

+ (void)document:(NSDocument *)document succeeded:(BOOL)didSucceed withCompletionHandler:(void (^)(BOOL didSucceed))completionHandler { completionHandler(didSucceed); Block_release(completionHandler);}

• And reuse them like this

[self saveDocumentWithDelegate:[self class] didSaveSelector:@selector(document:succeeded:withCompletionHandler:) contextInfo:Block_copy(^(BOOL didSave) { // Saving is done here. })];

20

Page 20: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Use File Packages

• Good for new document formats that support lots of attachments• NSFileWrapper!

■ Much improved in Snow Leopard■ Uses hard links as an optimization■ See the Mac OS 10.6 AppKit release notes

21

Page 21: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Autosaving

• Turn on autosaving in NSDocumentController

[[NSDocumentController sharedDocumentController] setAutosavingDelay:30];

• See the Mac OS 10.4 AppKit release notes for NSDocument methods to override and invoke

22

Page 22: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Autosaving

• See TextEdit for example

• /Developer/Examples/TextEdit

23

Page 23: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Exceptions and Error Handling

24

Page 24: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Exceptions Are for Programming Errors

• In general, exceptions shouldn’t be used for normal control flow■ Your tests should run with no exceptions thrown■ Unless you’re using them within your own subsystems

• Be careful about catching and not rethrowing■ Good at some subsystem boundaries

■ Grand Central Dispatch (libdispatch)■ Bad almost everywhere else■ At least log

25

Page 25: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Catching Exceptions in the Debugger

• Breaking on objc_exception_throw allows you to catch programming errors■ “Stop on Objective-C Exceptions”■ NSAccessibilityException

26

Page 26: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Reporting ExceptionsLet the user see them and report bugs

27

Page 27: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Reporting Exceptions

• Override -[NSApplication reportException:]

@implementation MyApplication

- (void)reportException:(NSException *)exception { @try {

// Show [exception reason] and [exception callStackSymbols] to the user // See demo source code for an example } @catch (NSException *e) { // Suppress exceptions from the above code }}

@end

28

Page 28: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Reporting ExceptionsSetting the custom NSApplication subclass

29

Page 29: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Errors Are for Showing to the User

• Used in a lot of places in Cocoa now• Meant to be “presentable” to the user• NSResponder has methods for presenting them

- (void)presentError:(NSError *)error modalForWindow:(NSWindow *)window delegate:(id)delegate didPresentSelector:(SEL)didPresentSelector contextInfo:(void *)contextInfo

- (BOOL)presentError:(NSError *)error;

30

Page 30: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Nib Tips

Corbin DunnCocoa Software Engineer

31

Page 31: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSWindowController and NSViewControllerMyWindowController *myWindowController =

[[MyWindowController alloc] initWithWindowNibName:@"Window"];

[_myWindowController.window makeKeyAndOrderFront:nil];

• Top level objects automatically released for you

32

Page 32: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Manually Loading Nibs

• An NSNib can be reused to create multiple instances from a nib

NSNib *nib = [[NSNib alloc] initWithNibNamed:@"NibName" bundle:nil];

// self has an IBOutlet called _secondWindow[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

// The IBOutlet is overwritten on the second call[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

33

Page 33: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Manually Loading Nibs

• An NSNib can be reused to create multiple instances from a nib

NSNib *nib = [[NSNib alloc] initWithNibNamed:@"NibName" bundle:nil];

// self has an IBOutlet called _secondWindow[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

// The IBOutlet is overwritten on the second call[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

34

Page 34: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Manually Loading Nibs

• An NSNib can be reused to create multiple instances from a nib

NSNib *nib = [[NSNib alloc] initWithNibNamed:@"NibName" bundle:nil];

// self has an IBOutlet called _secondWindow[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

// The IBOutlet is overwritten on the second call[nib instantiateNibWithOwner:self topLevelObjects:nil];[_secondWindow makeKeyAndOrderFront:nil];

35

Page 35: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

The Owner and -awakeFromNib- (void)awakeFromNib { static NSInteger awakeFromNibCount = 0; NSLog(@"awakeFromNib called %ld", (long)++awakeFromNibCount);}

2010-05-12 14:49:42.948 NibLoading[11709:a0f] awakeFromNib called 12010-05-12 14:49:44.871 NibLoading[11709:a0f] awakeFromNib called 22010-05-12 14:49:44.878 NibLoading[11709:a0f] awakeFromNib called 3

36

Page 36: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Tips on Manually Loading Nibs

• You must release all top-level objects

37

Page 37: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

SplitViews, PathControls, and NSCollectionView

Kevin PerryCocoa Software Engineer

38

Page 38: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSResponder

NSViewController

Nib-Based NSCollectionViewItem

NSObject

NSCollectionViewItem

Prototype View

Item View Item View Item View

Item View

Prototype View

Item View Item View

Nib Instance

Nib Instance

Nib Instance

39

Page 39: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

File’s Owner(NSCollectionViewItem)

View nib

Nib-Based NSCollectionViewItem

Main nib

NSCollectionView

Item Prototype

Prototype View

Bindings

Nib Name

40

Page 40: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Nib-Based NSCollectionViewItem

• Leopard

- (id)copyWithZone:(NSZone *)zone { id copy = [[[self class] alloc] init]; NSNib *viewNib = // ... [viewNib instantiateNibWithOwner:copy topLevelObjects:NULL]; return copy;}

- (void)loadView { // Do nothing.}

41

Page 41: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSPathControl

42

Page 42: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSPathControl

• More than file paths-[NSPathControl setStringValue:]-[NSPathComponentCell setImage:]

• Custom drawing+[NSPathCell pathComponentCellClass]-[NSPathCell rectOfPathComponentCell:withFrame:inView:]

43

Page 43: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSSplitView

44

Page 44: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

• Leopard and before

NSSplitView

- (void)splitView:(NSSplitView *)sv resizeSubviewsWithOldSize:(NSSize)size { CGFloat deltaWidth = NSWidth([sv bounds]) - size.width; CGFloat height = NSHeight([sv bounds]); NSView *firstView = [[sv subviews] objectAtIndex:0]; NSSize firstSize = [firstView frame].size; firstSize.height = height; [firstView setFrameSize:firstSize]; NSView *middleView = [[sv subviews] objectAtIndex:1]; NSSize middleSize = [middleView frame].size; middleSize.width += deltaWidth; middleSize.height = height; [middleView setFrameSize:middleSize]; NSView *lastView = [[sv subviews] lastObject]; NSRect lastFrame = [lastView frame]; lastFrame.origin.x += deltaWidth; lastFrame.size.height = height; [lastView setFrame:lastFrame];}

45

Page 45: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

• Snow Leopard

NSSplitView

- (BOOL)splitView:(NSSplitView *)sv shouldAdjustSizeOfSubview:(NSView *)view { return view == middleView;}

46

Page 46: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Objective-C Tips

Corbin DunnCocoa Software Engineer

47

Page 47: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Static “Methods” in Objective-CWait a minute—what and why?

@implementation AppDelegate

static void _processData(AppDelegate *self, NSInteger i) { // We have a private _window ivar NSLog(@"Processing %d in window %@", i, self->_window);}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { for (NSInteger i = 0; i < 200; i++) { _processData(self, i); }}

@end

48

Page 48: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

OBJC_HELPexport OBJC_HELP=YES

corbin@neeb:/Applications/TextEdit.app/Contents/MacOS/TextEdit objc[2569]: Objective-C runtime debugging. Set variable=YES to enable.objc[2569]: OBJC_HELP: describe available environment variablesobjc[2569]: OBJC_PRINT_OPTIONS: list which options are setobjc[2569]: OBJC_PRINT_IMAGES: log image and library names as they are loadedobjc[2569]: OBJC_PRINT_LOAD_METHODS: log calls to class and category +load methodsobjc[2569]: OBJC_PRINT_INITIALIZE_METHODS: log calls to class +initialize methodsobjc[2569]: OBJC_PRINT_RESOLVED_METHODS: log methods created by +resolveClassMethod: and +resolveInstanceMethod:objc[2569]: OBJC_PRINT_CLASS_SETUP: log progress of class and category setupobjc[2569]: OBJC_PRINT_PROTOCOL_SETUP: log progress of protocol setupobjc[2569]: OBJC_PRINT_IVAR_SETUP: log processing of non-fragile ivarsobjc[2569]: OBJC_PRINT_VTABLE_SETUP: log processing of class vtablesobjc[2569]: OBJC_PRINT_VTABLE_IMAGES: print vtable images showing overridden

methodsobjc[2569]: OBJC_PRINT_CACHE_SETUP: log processing of method cachesobjc[2569]: OBJC_PRINT_FUTURE_CLASSES: log use of future classes for toll-free bridgingobjc[2569]: OBJC_PRINT_RTP: log initialization of the Objective-C runtime pagesobjc[2569]: OBJC_PRINT_GC: log some GC operationsobjc[2569]: OBJC_PRINT_PREOPTIMIZATION: log preoptimization courtesy of dyld shared cacheobjc[2569]: OBJC_PRINT_CXX_CTORS: log calls to C++ ctors and dtors for instance variablesobjc[2569]: OBJC_PRINT_EXCEPTIONS: log exception handlingobjc[2569]: OBJC_PRINT_EXCEPTION_THROW: log backtrace of every objc_exception_throw()objc[2569]: OBJC_PRINT_ALT_HANDLERS: log processing of exception alt handlersobjc[2569]: OBJC_PRINT_REPLACED_METHODS: log methods replaced by category implementationsobjc[2569]: OBJC_PRINT_DEPRECATION_WARNINGS: warn about calls to deprecated runtime functionsobjc[2569]: OBJC_DEBUG_UNLOAD: warn about poorly-behaving bundles when unloadedobjc[2569]: OBJC_DEBUG_FRAGILE_SUPERCLASSES: warn about subclasses that may have been broken by subsequent changes to superclassesobjc[2569]: OBJC_DEBUG_FINALIZERS: warn about classes that implement -dealloc but not -finalizeobjc[2569]: OBJC_DEBUG_NIL_SYNC: warn about @synchronized(nil), which does no synchronizationobjc[2569]: OBJC_DEBUG_NONFRAGILE_IVARS: capriciously rearrange non-fragile ivarsobjc[2569]: OBJC_DEBUG_ALT_HANDLERS: record more info about bad alt handler useobjc[2569]: OBJC_USE_INTERNAL_ZONE: allocate runtime data in a dedicated malloc zoneobjc[2569]: OBJC_DISABLE_GC: force GC OFF, even if the executable wants it onobjc[2569]: OBJC_DISABLE_VTABLES: disable vtable dispatchobjc[2569]: OBJC_DISABLE_PREOPTIMIZATION: disable preoptimization courtesy of dyld shared cache

49

Page 49: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Demo: Xcode Cocoa Tricks

Corbin DunnCocoa Software Engineer

50

Page 50: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

The Secret Life ofYour App’s User Interface

James DempseyCocoa Software Engineer

51

Page 51: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

52

Page 52: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

53

Page 53: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

54

Page 54: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

55

Page 55: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Buttons

Ruler

Text Area

Window

Scroll Area

Resize Box

Static TextImage

Ruler MarkerRuler Markers

Segmented Control

Menu Buttons

Scroller

56

Page 56: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Scrollernot enabledall content visible

“Gettysburg Address”“close” “minimize” “zoom”

“align Left”

“center”

“justify”

“align right”

“Styles”

“tail indent”“head indent”

“left”“center”“right”“decimal”

“Lists”

“Spacing”

Selected Text“Four score and seven years ago”

57

Page 57: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

58

Page 58: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Accessibility Client APIs

NSAccessibility

Reveal Custom Views

Describe UI Elements

Use Standard ControlsVoiceOver Your App

59

Page 59: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Demo: Easy Image Descriptions

James DempseyCocoa Software Engineer

60

Page 60: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Demo: Automated UI Testing

James DempseyCocoa Software Engineer

61

Page 61: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Blurry Views

Corbin DunnCocoa Software Engineer

62

Page 62: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Have You Seen Blurry Views?

NOTE: Image simulated to hide detail

63

Page 63: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Have You Seen Blurry Views?

64

Page 64: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Cause: Non Integral Pixel Alignment

x = 200.5, y = 200.5

65

Page 65: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Two Solutions: First—centerScanRect:

NSRect frame = [_imageView frame];

frame = [[_imageView superview] centerScanRect:frame];

[_imageView setFrame:frame];

66

Page 66: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Two Solutions: Second—Explicit

NSRect frame = [_imageView frame];

// Convert to pixel spaceframe = [_imageView.superview convertRectToBase:frame];

// At this point we can floor/round/ceilframe.origin.x = floor(frame.origin.x);frame.origin.y = floor(frame.origin.y);

// Convert back to the view's coordinate spaceframe = [_imageView.superview convertRectFromBase:frame];

[_imageView setFrame:frame];

67

Page 67: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Image Tips

Ken FerryCocoa Software Engineer

68

Page 68: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Flippedness

69

Page 69: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

FlippednessDrawing images right side up

-[NSImage drawInRect:fromRect:operation:fraction:respectFlipped:hints:]

-[NSImage compositeToPoint:fromRect:operation:fraction:]

-[NSImage setFlipped:]

70

Page 70: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

MutationNon-local effects

[image setFlipped:YES]

71

Page 71: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

MutationMutate as part of set up

[[NSImage alloc] init]set…set…set…

[image copy]set…set…set…

72

Page 72: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

PerformanceSame drawing, same image

-drawRect: { [[NSImage alloc] init]…}

[cell setImage:]

73

Page 73: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSImage Wrap Up

• Use -draw methods respecting flippedness• Mutate only as part of setup• Perform identical drawing with identical images

Text

10.6 AppKit release notesNSImage in SnowLeopard, WWDC 2009

74

Page 74: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

More Table Tips

Corbin DunnCocoa Software Engineer

75

Page 75: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Variable Row Heights

76

Page 76: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row { NSCell *cell = [tableView preparedCellAtColumn:0 row:row]; NSRect constrainedBounds = NSMakeRect(0, 0, _tableColumnWidth, CGFLOAT_MAX);

NSSize naturalSize = [cell cellSizeForBounds:constrainedBounds]; return naturalSize.height;

}

Use the cell to find the preferred heightVariable Row Heights Delegate

77

Page 77: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

- (void)tableViewColumnDidResize:(NSNotification *)notification {

_tableColumnWidth = [[_tableView tableColumnWithIdentifier:@"ID"] width]; NSIndexSet *allRows = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, _tableView.numberOfRows)] [_tableView noteHeightOfRowsWithIndexesChanged:allRows];

}

Cache the Table Column’s widthVariable Row Heights Delegate

78

Page 78: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Summary

• Lots of tips• Hopefully you learned a few new things• Download the sample code that illustrates most of these tips

79

Page 79: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Cocoa Lab Application Frameworks Lab CTuesday 3:15PM–6:30PM

Labs

Cocoa Lab Application Frameworks Lab DThursday 2:00PM–4:15PM

80

Page 80: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Song

James Dempsey and the Breakpoints

81

Page 81: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

82

Page 82: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

83

Page 83: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

84

Page 84: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

(Book Learnin’ Too)

85

Page 85: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

GoodBehavior

86

Page 86: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

87

Page 87: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

ACK!

88

Page 88: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

89

Page 89: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

“When in doubt…-retain”

90

Page 90: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

0x102E2CC300x102E2C9500x100408B000x102E2D2D00x102E2C4500x100508EB0

Help!

91

Page 91: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

while ([self retainCount] > 0) {[self release];}

92

Page 92: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Memory Management Programming Guidefor CocoaPerformance

93

Page 93: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

94

Page 94: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];// Hold Me

95

Page 95: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];

// Use Me

// Hold Me

[myObj doStuff];

96

Page 96: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];

[myObj release];

// Use Me

// Hold Me

// Release Me

[myObj doStuff];

97

Page 97: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];

[myObj release];

// Use Me

// Hold Me

// Release Me

[myObj doStuff];

98

Page 98: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];

[myObj release];

// Use Me

// Hold Me

// Release Me

[myObj doStuff];

99

Page 99: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObj retain];

[myObj release];

// Use Me

// Hold Me

// Release Me

[myObj doStuff];

100

Page 100: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

101

Page 101: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

BOOM!

102

Page 102: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

NSMutableString *upperString = [NSMutableString string];

for (NSUInteger i = 0; i < [str length]; i++) {

unichar theChar = [str characterAtIndex:i];

if (theChar > 96 && theChar < 123) theChar -= 32;

[upperString appendString:[NSString stringWithCharacters:&theChar length:1]];

}

103

Page 103: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

1472 page printed document

104

Page 104: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

105

Page 105: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[myObject __secretPrivateMethod];

106

Page 106: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

107

Page 107: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

I BREAK FOR

ENCAPSULATION

108

Page 108: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

109

Page 109: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Code SnippetHall of Shame

(Abridged)

110

Page 110: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[(NSMutableArray *)[myView subviews] sortUsingSelector:@selector(sortViews:)];

-subviews returns NSArray

This violates the contract in a big way

111

Page 111: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

// 1. Get a menuNSMenuItem *aMenu = [mainMenu itemAtIndex:index];

// 2. Pretend its not a menuNSMenu *someMenu = (NSMenu *)aMenu;

// 3. Hilarity ensues:-[NSMenuItem supermenu]: unrecognized selector sent to instance 0x10010ca70

Don’t overestimate the power of typecasting

112

Page 112: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

@synchronized (sharedDict) {

}

sharedDict =[[NSDictionary alloc]

initWithObject:@"foo" forKey:@"bar"];

static NSDictionary *sharedDict = nil;

Did this really just synchronize on nil?

113

Page 113: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

[self dealloc];

-release perhaps?

114

Page 114: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

115

Page 115: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

116

Page 116: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Anti-Patterns

117

Page 117: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

Bill DudneyApplication Frameworks [email protected]

Apple Developer Forumshttp://devforums.apple.com

More Information

118

Page 118: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

119

Page 119: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

120

Page 120: Cocoa Tips and Tricks - Huihoodocs.huihoo.com/apple/wwdc/2010/session_107__cocoa_tips_and_tricks.pdfCool Cocoa code Corbin Dunn Cocoa Software Engineer Cocoa Tips and Tricks 2. Introduction

121