ARCでめちゃモテiOSプログラマー

16,647 views
16,453 views

Published on

Published in: Technology, Business
0 Comments
27 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
16,647
On SlideShare
0
From Embeds
0
Number of Embeds
4,025
Actions
Shares
0
Downloads
67
Comments
0
Likes
27
Embeds 0
No embeds

No notes for slide

ARCでめちゃモテiOSプログラマー

  1. 1. ARC iOS (id:ninjinkun / @ninjinkun)
  2. 2. • Cocoa Touch• ARC• ARC•• ARC•• Tips• GC
  3. 3. Cocoa Touch • retain / relase -(void)setName:(NSString *)newName { name = [newName retain]; } -(void)dealloc { [name release]; [super dealloc]; 1 3 0 } • Ownership • Ownership retain • Ownership release • 0
  4. 4. Cocoa TouchAutorelase • • autorelease • release • / •
  5. 5. Cocoa TouchAutorelase • • autorelease • release • / • -(void)buildNewName { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease]; [array addObject:@"hoge"]; [array addObject:@"fuga"]; [array addObject:@"piyo"]; name =[array componentsJoinedByString:@","]; [pool drain]; }
  6. 6. ARC• Automatic Reference Counting•• iOS 5 / Mac OS X 10.7
  7. 7. ARC• •• (GC)• Static Analyzer
  8. 8. ARC• @interface NonARCObject : NSObject { NSString *name; } -(id)initWithName:(NSString *)name; @end @implementation NonARCObject -(id)initWithName:(NSString *)newName { self = [super init]; if (self) { name = [newName retain]; } return self; } -(void)dealloc { [name release]; [super dealloc]; } @end
  9. 9. ARC• @interface ARCObject : NSObject { NSString *name; } -(id)initWithName:(NSString *)name; @end @implementation ARCObject -(id)initWithName:(NSString *)newName { self = [super init]; if (self) { name = newName; } return self; } @end
  10. 10. ARCARC • • … • • • •
  11. 11. ARC __strong•• Ownership• • retain, release -(void)buildNewName { { __strong NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject:@"hoge"]; [array addObject:@"fuga"]; [array addObject:@"piyo"]; name =[array componentsJoinedByString:@","]; } } !
  12. 12. ARC __strong• • retain, dealloc relase @interface ARCUser : NSObject { __strong NSString *name; } @end @implementation ARCUser -(id)initWithName:(NSString *)newName { self = [super init]; if (self) { name = newName; // [newName retain] } return self; } -(void)dealloc { // [name release] } @end
  13. 13. ARC __weak• __weak • • Ownership • nil • • iOS 5 @interface ARCUser : NSObject { __weak id delegate; } @end
  14. 14. ARC __unsafe_unretainded• • assign•• iOS 4.3 @interface ARCUser : NSObject { __unsafe_unretained id delegate; } @end
  15. 15. ARC __autoreleasing• autorelase•• @autorelasepool { } -(NSArray *)comvertImageToJpeg:(NSArray *)files { NSMutableArray *dataStore = [NSMutableArray array]; @autoreleasepool { for (NSString *filePath in files) { __autoreleasing UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath]; NSData *data = UIImageJPEGRepresentation(image, 1.0); [dataStore addObject:data]; } } return [dataStore copy]; }
  16. 16. ARC• retain, release, autorelase • retainCount• [super dealloc] • dealloc -(void)dealloc { delegate = nil; }• C __bridge NSString *str = @"hogehoge"; CFStringRef strRef = (__bridge CFStringRef)str; CFStringRef strRef = (__bridge_retained CFStringRef)str;
  17. 17. • ARC __strong• __strong __strong __strong __strong __strong __strong
  18. 18. • iOS 5 __weak• iOS 4.3 __unsafe_unretaind• nil __strong __weak __strong __strong __strong
  19. 19. ARC•
  20. 20. ARCretain / relase • -S • • _objc_release() • _objc_retain() • _objc_retainAutoreleasedReturnValue()
  21. 21. ARC__weak • _objc_storeWeak() • 0 _objc_destroyWeak() • • nil • This document is licensed to ninjin@mac.com.
  22. 22. Blocks • ARC • self ? • • release • BlocksKit UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addEventHandler:^(id sender) { [self showPhotoPickerView]; } forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
  23. 23. Blocks • UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; __unsafe_unretained id _self = self; // !? [button addEventHandler:^(id sender) { [_self showPhotoPickerView]; } forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
  24. 24. Blocks • UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; __unsafe_unretained id _self = self; // !? [button addEventHandler:^(id sender) { !? [_self showPhotoPickerView]; } forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
  25. 25. TipsARC • -fno-objc-arc
  26. 26. TipsARC • Static Library • Static Library • Workspace
  27. 27. Tips • ARC • iOS 5 __weak • Blocks • UI • UI Blocks
  28. 28. GC• GC • iOS •• • CPU
  29. 29. • ARC• ( )• __weak• GC • • ( )• ARC

×