objective-c 기초 2 dongjoo kim. instagram initial release - 2010.10.6 users - 10 million ( in...
DESCRIPTION
Agenda Object Typing Reference Counting - retain / release - autoreleaseTRANSCRIPT
Objective-C 기초 2
DongJoo Kim
InstagramInitial Release - 2010.10.6
Users - 10 Million ( in 355days)
Funding - 약 80 억원
기업가치 - 350 억원 ( 추정 )
Agenda
Object Typing
Reference Counting - retain / release - autorelease
Cocoa TouchProvides a very rich starting point for exploring app design
Main Frameworks - Foundation Kit - UIKit - MapKit
MVC ModelControlle
r
Model View
Controller - UI LogicView – UI Component 배치Model - Data
Object Typing@interface Vehicle - (void)move;@interface Ship : Vehicle - (void)shoot;@end
Ship *s = [[Ship alloc] init];[s shoot];[s move];// 다음 코드에 문제가 있을까요 ?
Object Typing@interface Vehicle - (void)move;@interface Ship : Vehicle - (void)shoot;@end
Ship *s = [[Ship alloc] init];[s shoot];[s move];Vehicle *v = s;// 다음 코드에 문제가 있을까요 ?
Object Typing@interface Vehicle - (void)move;@interface Ship : Vehicle - (void)shoot;@end
Ship *s = [[Ship alloc] init];[s shoot];[s move];Vehicle *v = s;[v shoot];// 다음 코드에 문제가 있을까요 ?
Object Typing 3Vehicle *v = s;[v shoot];// 다음 코드에 문제가 있을까요 ?
Protocols@protocol Foo - (void) doSomething;@optional - (int ) getSomething;@required - (NSArray*)getManySomethings:(int)howMany;@end
특정메소드를 구현하라 / 강제 , 권유 하는것
구현안하면 컴파일 워닝이 발생
UITableViewDelegate
– tableView:heightForRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
Reference CountingWhy Reference Counting - No Garbage Collection
Object 를 이용할때 Reference Count 증가
Object 를 더 이상 이용하지 않을때 Reference count 감소
Reference Count 가 0 가 될때 해당 object 는 메모리에서 제거됨
Reference CountingWhy Reference Counting - No Garbage Collection - 사실 있음 !!!! autoreleaseObject 를 이용할때 Reference Count 증가
Object 를 더 이상 이용하지 않을때 Reference count 감소
Reference Count 가 0 가 될때 해당 object 는 메모리에서 제거됨
Reference Count 증가예외 ) new, alloc, copy 혹은 factory 함수등을 통해 Object 를 가지고 올때 -> Reference Count 증가할 필요 X그외에는 Object 에게 retain 메세지를 전송한다 . ( 아마 = 을 사용해서 object 를 갖고 올때 )
예제와 함께 보기NSNumber *myInt =[NSNumber numberWithInteger:100];NSNumber *myInt2 ;NSMutableArray *myArr = [NSMutableArray arrayWithCapacity:5];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]);
[myArr addObject:myInt];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]);
myInt2 = myInt; NSLog(@”myInt2 is %lx”,(unsigned long)[myInt2 retainCount]);
[myInt retain];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]); NSLog(@”myInt2 is %lx”,(unsigned long)[myInt2 retainCount]);
예제와 함께 보기 - 정답NSNumber *myInt =[NSNumber numberWithInteger:100];NSNumber *myInt2 ;NSMutableArray *myArr = [NSMutableArray arrayWithCapacity:5];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]);
[myArr addObject:myInt];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]);
myInt2 = myInt; NSLog(@”myInt2 is %lx”,(unsigned long)[myInt2 retainCount]);
[myInt retain];NSLog(@”myInt is %lx”,(unsigned long)[myInt retainCount]); NSLog(@”myInt2 is %lx”,(unsigned long)[myInt2 retainCount]);
1
2
2
33
AutoRelease - 데스노트지금은 안죽지만 언젠가 죽음 . (pool 이 release 될때 )
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString* testString = [[[NSString alloc] initWithString:@"test"] autorelease];
[pool release];
Reference Counting 요약1 ) alloc, copy, new 들어가면 retain 했다고 생각하라
queue = [NSOperationQueue alloc] init];// …[queue release];
2 ) 결자해지 -> retain(alloc) 했으면 release 하라
[queue retain];// …[queue release];3 ) 클래스 메서드가 객체를 생성해줄때는 autorelease이니 신경꺼라 .img = [UIImage imageNamed:@”test.png”];
4 ) 컬렉션 (NSMutableDictionary, NSMutableArray) 에 어객체를 넣을때는 (retain) 뺄때는 (release) 자동호출된다 ..
Reference Counting 요약5 ) 유저가 만든 메소드가 객체를 리턴해주는 경우는 autorelease 를 써서 보내자 . ( 그냥 release 하면 큰일나지요 )
-(NSString*) findTopPlayer { NSString *ret = [[top objectAtIndex:0] copy]; [ret autorelease]; return ret; }
클래스 메소드와 Reference Counting - 한 번 더 요약 !!!
클래스 메소드 이름에 alloc 이 들어가는 경우 메소드 호출하는 곳에서 releaseNSString *text = [[NSString alloc] initWithString@”Test”];클래스 메소드 이름이 안들어가는 경우autorelease 니까 신경꺼 !NSString *text = [NSString stringWithString@”Test”];
Setter/Getter 와 dot notation
같습니다 ! // setter[myView setHidden:YES];myView.hidden = YES;
// getterBOOL isHidden = [myView hidden];BOOL isHidden = myView.hidden;
Property 만 잘 설정해주신 뒤에는 dot notation 으로 사용하시면 됨