More Related Content
Similar to ARCでめちゃモテiOSプログラマー (20)
More from Satoshi Asano (9)
ARCでめちゃモテiOSプログラマー
- 1. ARC iOS
(id:ninjinkun / @ninjinkun)
- 2. • Cocoa Touch
• ARC
• ARC
•
• ARC
•
• Tips
• GC
- 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
- 5. Cocoa Touch
Autorelase
•
• 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. ARC
• Automatic Reference Counting
•
• iOS 5 / Mac OS X 10.7
- 7. ARC
•
•
• (GC)
• Static Analyzer
- 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. 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
- 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. 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. ARC
__weak
• __weak
•
• Ownership
• nil
•
• iOS 5
@interface ARCUser : NSObject {
__weak id delegate;
}
@end
- 14. ARC
__unsafe_unretainded
•
• assign
•
• iOS 4.3
@interface ARCUser : NSObject {
__unsafe_unretained id delegate;
}
@end
- 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. 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. • ARC __strong
• __strong
__strong
__strong
__strong __strong
__strong
- 18. • iOS 5 __weak
• iOS 4.3 __unsafe_unretaind
• nil
__strong
__weak
__strong __strong
__strong
- 20. ARC
retain / relase
• -S
•
• _objc_release()
• _objc_retain()
• _objc_retainAutoreleasedReturnValue()
- 21. ARC
__weak
• _objc_storeWeak()
• 0 _objc_destroyWeak()
•
• nil
•
This document is licensed to ninjin@mac.com.
- 22. Blocks
• ARC
• self ?
•
• release
• BlocksKit
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addEventHandler:^(id sender) {
[self showPhotoPickerView];
} forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- 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. Blocks
•
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
__unsafe_unretained id _self = self; // !?
[button addEventHandler:^(id sender) {
!?
[_self showPhotoPickerView];
} forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- 26. Tips
ARC
• Static Library
• Static Library
• Workspace
- 27. Tips
• ARC
• iOS 5 __weak
• Blocks
• UI
• UI Blocks
- 28. GC
• GC
• iOS
•
•
• CPU
- 29. • ARC
• ( )
• __weak
• GC
•
• ( )
• ARC