1 / 21

Objective-C 기초 2

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. Cocoa Touch.

irving
Télécharger la présentation

Objective-C 기초 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objective-C 기초 2 DongJoo Kim

  2. Instagram Initial Release - 2010.10.6 Users - 10 Million ( in 355days) Funding - 약80억원 기업가치 - 350억원(추정)

  3. Agenda Object Typing Reference Counting - retain / release - autorelease

  4. Cocoa Touch Provides a very rich starting point for exploring app design Main Frameworks - Foundation Kit - UIKit - MapKit

  5. MVC Model Controller Model View Controller - UI Logic View – UI Component 배치 Model - Data

  6. Object Typing @interface Vehicle - (void)move; @interface Ship : Vehicle - (void)shoot; @end Ship *s = [[Ship alloc] init]; [s shoot]; [s move]; //다음 코드에 문제가 있을까요?

  7. Object Typing @interface Vehicle - (void)move; @interface Ship : Vehicle - (void)shoot; @end Ship *s = [[Ship alloc] init]; [s shoot]; [s move]; Vehicle *v = s; //다음 코드에 문제가 있을까요?

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

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

  10. Protocols @protocol Foo - (void) doSomething; @optional - (int ) getSomething; @required - (NSArray*)getManySomethings:(int)howMany; @end 특정메소드를 구현하라 / 강제, 권유 하는것 구현안하면 컴파일 워닝이 발생

  11. UITableViewDelegate – tableView:heightForRowAtIndexPath: – tableView:didSelectRowAtIndexPath:

  12. Reference Counting Why Reference Counting - No Garbage Collection Object 를 이용할때 Reference Count 증가 Object 를 더 이상 이용하지 않을때 Reference count 감소 Reference Count 가 0 가 될때 해당 object는 메모리에서 제거됨

  13. Reference Counting Why Reference Counting - No Garbage Collection - 사실 있음!!!! autorelease Object 를 이용할때 Reference Count 증가 Object 를 더 이상 이용하지 않을때 Reference count 감소 Reference Count 가 0 가 될때 해당 object는 메모리에서 제거됨

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

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

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

  17. AutoRelease - 데스노트 지금은 안죽지만 언젠가 죽음. (pool 이 release 될때) NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString* testString = [[[NSString alloc] initWithString:@"test"] autorelease]; [pool release];

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

  19. Reference Counting 요약 5 ) 유저가 만든 메소드가 객체를 리턴해주는 경우는 autorelease 를 써서 보내자. ( 그냥 release 하면 큰일나지요) -(NSString*) findTopPlayer { NSString *ret = [[top objectAtIndex:0] copy]; [ret autorelease]; return ret; }

  20. 클래스 메소드와 Reference Counting - 한 번 더 요약 !!! 클래스 메소드 이름에 alloc 이 들어가는 경우 메소드 호출하는 곳에서 release NSString *text = [[NSString alloc] initWithString@”Test”]; 클래스 메소드 이름이 안들어가는 경우 autorelease 니까 신경꺼! NSString *text = [NSString stringWithString@”Test”];

  21. Setter/Getter 와 dot notation 같습니다! // setter [myView setHidden:YES]; myView.hidden = YES; // getter BOOL isHidden = [myView hidden]; BOOL isHidden = myView.hidden; Property 만 잘 설정해주신 뒤에는 dot notation 으로 사용하시면 됨

More Related