objective-c 기초 2 dongjoo kim. instagram initial release - 2010.10.6 users - 10 million ( in...

21
Objective-C 기기 2 DongJoo Kim

Upload: britney-higgins

Post on 08-Jan-2018

224 views

Category:

Documents


7 download

DESCRIPTION

Agenda Object Typing Reference Counting - retain / release - autorelease

TRANSCRIPT

Page 1: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Objective-C 기초 2

DongJoo Kim

Page 2: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

InstagramInitial Release - 2010.10.6

Users - 10 Million ( in 355days)

Funding - 약 80 억원

기업가치 - 350 억원 ( 추정 )

Page 3: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Agenda

Object Typing

Reference Counting - retain / release - autorelease

Page 4: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Cocoa TouchProvides a very rich starting point for exploring app design

Main Frameworks - Foundation Kit - UIKit - MapKit

Page 5: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

MVC ModelControlle

r

Model View

Controller - UI LogicView – UI Component 배치Model - Data

Page 6: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Object Typing@interface Vehicle - (void)move;@interface Ship : Vehicle - (void)shoot;@end

Ship *s = [[Ship alloc] init];[s shoot];[s move];// 다음 코드에 문제가 있을까요 ?

Page 7: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Object Typing@interface Vehicle - (void)move;@interface Ship : Vehicle - (void)shoot;@end

Ship *s = [[Ship alloc] init];[s shoot];[s move];Vehicle *v = s;// 다음 코드에 문제가 있을까요 ?

Page 8: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

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];// 다음 코드에 문제가 있을까요 ?

Page 9: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Object Typing 3Vehicle *v = s;[v shoot];// 다음 코드에 문제가 있을까요 ?

Page 10: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Protocols@protocol Foo - (void) doSomething;@optional - (int ) getSomething;@required - (NSArray*)getManySomethings:(int)howMany;@end

특정메소드를 구현하라 / 강제 , 권유 하는것

구현안하면 컴파일 워닝이 발생

Page 11: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

UITableViewDelegate

– tableView:heightForRowAtIndexPath:

– tableView:didSelectRowAtIndexPath:

Page 12: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Reference CountingWhy Reference Counting - No Garbage Collection

Object 를 이용할때 Reference Count 증가

Object 를 더 이상 이용하지 않을때 Reference count 감소

Reference Count 가 0 가 될때 해당 object 는 메모리에서 제거됨

Page 13: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Reference CountingWhy Reference Counting - No Garbage Collection - 사실 있음 !!!! autoreleaseObject 를 이용할때 Reference Count 증가

Object 를 더 이상 이용하지 않을때 Reference count 감소

Reference Count 가 0 가 될때 해당 object 는 메모리에서 제거됨

Page 14: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Reference Count 증가예외 ) new, alloc, copy 혹은 factory 함수등을 통해 Object 를 가지고 올때 -> Reference Count 증가할 필요 X그외에는 Object 에게 retain 메세지를 전송한다 . ( 아마 = 을 사용해서 object 를 갖고 올때 )

Page 15: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

예제와 함께 보기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]);

Page 16: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

예제와 함께 보기 - 정답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

Page 17: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

AutoRelease - 데스노트지금은 안죽지만 언젠가 죽음 . (pool 이 release 될때 )

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString* testString = [[[NSString alloc] initWithString:@"test"] autorelease];

[pool release];

Page 18: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

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) 자동호출된다 ..

Page 19: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Reference Counting 요약5 ) 유저가 만든 메소드가 객체를 리턴해주는 경우는 autorelease 를 써서 보내자 . ( 그냥 release 하면 큰일나지요 )

-(NSString*) findTopPlayer { NSString *ret = [[top objectAtIndex:0] copy]; [ret autorelease]; return ret; }

Page 20: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

클래스 메소드와 Reference Counting - 한 번 더 요약 !!!

클래스 메소드 이름에 alloc 이 들어가는 경우 메소드 호출하는 곳에서 releaseNSString *text = [[NSString alloc] initWithString@”Test”];클래스 메소드 이름이 안들어가는 경우autorelease 니까 신경꺼 !NSString *text = [NSString stringWithString@”Test”];

Page 21: Objective-C 기초 2 DongJoo Kim. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약 80 억원 기업가치 - 350 억원 ( 추정 )

Setter/Getter 와 dot notation

같습니다 ! // setter[myView setHidden:YES];myView.hidden = YES;

// getterBOOL isHidden = [myView hidden];BOOL isHidden = myView.hidden;

Property 만 잘 설정해주신 뒤에는 dot notation 으로 사용하시면 됨