Академический Документы
Профессиональный Документы
Культура Документы
Greg Parker
Runtime Wrangler
Legacy
Modern
Legacy
Modern
32-bit Mac OS
Must be 64-bit only for some features May now use new features everywhere
Advanced Techniques
Writing code Not writing code Not executing code
10
Advanced Techniques
Writing code
11
12
@interface PetShopView : NSView { @private NSArray *kittens; NSArray *puppies; } @property (readwrite) int puppyFood; -(void) feedSnakeWith:(id)food; @end PetShopView *shop; shop.puppyFood = 0; [shop feedSnakeWith:shop.kittens];
13
@interface PetShopView : NSView { @private NSArray *kittens; NSArray *puppies; } @property (readonly) int puppyFood; @end
@interface PetShopView ()
14
15
16
@synthesize
PetShopView.h PetShopView.m
@interface PetShopView : NSView { @private int puppyFood; } @property (readwrite) int puppyFood; @end
@implementation PetShopView @synthesize puppyFood; -(id) init { self = [super init]; self->puppyFood = 10; return self; } @end
17
@synthesize by Default
Modern runtime only LLVM Compiler only
18
Alternatives to @synthesize
Write accessor methods by hand @dynamic with message forwarding @dynamic with dynamic method resolution @dynamic with NSManagedObject
19
Weak Linking
20
@interface MyController : UIPopoverController // crashes @end MyController *obj = [[MyController alloc] init]; // sorry
21
@interface MyController : UIPopoverController // OK @end MyController *obj = [[MyController alloc] init]; if (obj) { // OK }
22
23
Block Esoterica
24
Block Esoterica
Block memory in action Copying blocks __block storage variables
25
Copying
__block int shared; int captured = 10; block2: const int captured = 10;
Stack
captured = 10; block1: block1 = ^{ const int captured = 10; shared += captured; }; captured = 20; block2 = [block1 copy]; block3 = [block1 copy];
Function()
Heap
26
Copying
__block int shared; int captured = 10; block2: block1: const int captured ==10; const int captured 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block1 copy];
Function()
Heap
27
Copying
__block int shared; int captured = 20; 10; block2: block1: const int captured ==10; const int captured 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block1 copy];
Function()
Heap
28
Copying
__block int shared; int captured = 20; block1: block3: int captured = 10; const const int captured = 10; block2: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block1 copy];
Function()
Heap
29
Copying
__block int shared; int captured = 20; block1: const int captured = 10; block2: const int captured = 10; block3: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block1 [block2 copy];
Function()
Heap
30
Copying
__block int shared; int captured = 20; block1: const int captured = 10; block2: block2 and block3: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block2 copy];
Function()
Heap
31
Copying
__block int shared; int captured = 20; block1: const int captured = 10; block2 and block3: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block2 copy];
Function()
Heap
32
Cleanup
__block int shared; int captured = 20; block1: const int captured = 10; block2 and block3: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block2 copy];
Function()
Heap
33
Cleanup
__block int shared; int captured = 20; block1: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block2 copy];
Function()
Heap
34
Cleanup
__block int shared; int captured = 20; block1: const int captured = 10; block2 and block3: const int captured = 10;
Stack
captured block1 = shared }; captured block2 = block3 = = 10; ^{ += captured; = 20; [block1 copy]; [block2 copy];
Function()
Heap
35
Cleanup
__block int shared; block2 and block3: const int captured = 10;
Stack
Function()
Heap
36
Cleanup
Stack
Function()
Heap
37
Block Copies
Outlive the creating function Runnable on another thread
38
How to Copy
[myBlock copy] and [myBlock release] Block_copy(myBlock) and Block_release(myBlock) Prefer the methods, not the functions
39
__block Basics
__block is a storage class __block variables are mutable __block variables are shared
40
__block Basics
__block variable values are not retained __block variables may be copied __block arrays are not allowed
41
__block Uses
Send values between different calls to the same block Send values to the blocks caller Beware of thread synchronization
42
43
44
Leak Detection
Finds Leaked memory Does not find Abandoned memory
45
Garbage Collection
Automatically deallocates Leaked memory Does not deallocate Abandoned memory
46
47
Demo
48
49
Optimizing Memory
Use leak detectors to find leaked memory
GC: works with unmanaged and collector-disabled memory GC: set AUTO_USE_TLC = NO
50
Summary
Runtime count reduced on iPhone OS Old language features given new twists Block objects demystified GC memory optimized
51
More Information
Michael Jurewitz
Developer Tools Evangelist jurewitz@apple.com
Documentation
The Objective-C Programming Language Objective-C Runtime Programming Guide http://developer.apple.com/
52
Related Sessions
Advanced Memory Analysis with Instruments Introducing Blocks and Grand Central Dispatch on iPhone (Repeat)
Presidio Thursday 11:30AM Pacific Heights Friday 2:00PM
53
Q&A
54
55
56