i os 2

65
iOS - 2 Sanghun Han EZNIX 솔루션개발 연구원

Upload: sanghoon-han

Post on 28-Jun-2015

251 views

Category:

Technology


2 download

DESCRIPTION

2번째 PPT

TRANSCRIPT

Page 1: I os 2

iOS - 2Sanghun Han

EZNIX 솔루션개발 연구원

Page 2: I os 2

Obj-C - Memory Management, Property, Protocol iOS - UIAlertView, Delegate

Page 3: I os 2

Memory Management

Page 4: I os 2

Memory?

• C : 작성자가 직접 관리

!

• Obj-C : Reference-Count 방식을 사용

• Obj-C 1.0에 도입, Obj-C 2.0에서도 사용 가능

• ARC

Page 5: I os 2

C Style

• malloc, calloc, realloc …

• free

Page 6: I os 2

Objective-C

• alloc

• retain

• release

• autorelease

Page 7: I os 2

Objective-C

• alloc +1

• retain +1

• release -1

• autorelease -1

Page 8: I os 2

Objective-C

• alloc +1 할당하면서 레퍼런스 카운트를 증가

• retain +1 레퍼런스 카운트를 증가

• release -1 레퍼런스 카운트를 감소

• autorelease -1 다 쓰고 나서 레퍼런스 카운트를 감소

Page 9: I os 2

Reference - Counting

• alloc

• 객체를 생성, 카운트는 1

• Ownership 개념

Page 10: I os 2

Reference - Counting

• retain

• 객체의 카운트를 ++

• 객체의 Owner가 하나 증가함

Page 11: I os 2

Reference - Counting

• release

• 객체의 카운트를 --

• 객체의 Owner가 하나 감소함

Page 12: I os 2

Reference - Counting

• autorelease

• 자동 해제

• NSAutoreleasePool과 연계

Page 13: I os 2

Reference - Counting

Page 14: I os 2

Reference - Counting

Page 15: I os 2

Autorelease

• 자동 해제 풀 • NSAutoreleasePool이 존재하지 않을 경우에 autorelease 메세지를 날리면, 런타임 에러가 발생

• 어플리케이션 생성 시 풀 생성 - 프로그램 종료 시 풀 해제

• 메모리 관리에 의미 없음, 지역 풀 사용 필요성

• 이벤트 풀 • NSApplication 이벤트 루프 관리

• 이벤트로 동작하는 루프 처리에서는 NSApplication이 자동 해제 풀을 관리

• 즉, 풀 생성을 따로 하지 않아도 자동으로 지역 풀 사용이 이루어짐

Page 16: I os 2

Convenience Constructor

• -(instancetype)initWithString:(NSString *)string;

• 객체 초기화 인스턴스 메서드

!

• +(instancetype)stringWithString:(NSString *)string;

• autorelease된 객체 리턴

• [[[NSString alloc] initWithString:@“~~”] autorelease]

Page 17: I os 2

instancetype keyword?

Page 18: I os 2

instancetype

Page 19: I os 2

instancetype

Page 20: I os 2

instancetype

Page 21: I os 2

ARC ?

Page 22: I os 2

ARC

• Automatic - Reference - Counting

Page 23: I os 2

컴파일러가 책임진다!

Page 24: I os 2

ARC

• retain, release, dealloc, autorelease 등을 사용자가 명시적으로 호출하지 않는다

!

• 컴파일러가 짝을 맞추어서 자동으로 코드를 생성한다.

!

• Compile Time Memory Management

Page 25: I os 2

ARC

• C Structure 내부의 Object는 관리되지 않음

!

• @property 속성이 달라짐

• retain, assign, copy / weak, strong

Page 26: I os 2

ARC

• retain, release 등은 어떤 형태로 호출하는 방법도 모두 금지된다.

• @selector(retain), @selector(release) 등도 금지된다.

!

• dealloc 메소드를 오버라이드 한다면, 코드 상에서 [super dealloc] 은 호출하지 않아야 한다.

Page 27: I os 2

ARC

• autorelease?

• NSAutoreleasePool

!

!

• @autoleasepool 로 변경!

Page 28: I os 2

ARC

• C structure 에서 객체를 멤버로 가져서는 안된다.

Page 29: I os 2

ARC

• Core Foundation 객체들은 ARC 적용이 되지 않는다.

!

• CFRetain, CFRelease 기존처럼 호출

!

• __bridge, __bridge_retained, __bridge_transfer

Page 30: I os 2

ARC• __bridge - 일반 캐스팅, 현상 유지

!

• __bridge_retained - Obj-C -> CF

• ARC 해제

• CFBridgingRetain(id X)

• __bridge_transfer - CF -> Obj-C

• ARC 적용

• CFBridgingRelease(CFTypeRef X)

Page 31: I os 2

ARC

Page 32: I os 2

Property

Page 33: I os 2

Property

• 접근자 !

• retain / assign / copy /// strong / weak

Page 34: I os 2

Declared Property

• @property ( 속성 ) Type varName;

• 선언부에 선언

!

• @synthesize

• 구현부에 선언

Page 35: I os 2

Declared Property

• Strong = Retain

• OwnerShip 부여, Retain ++

Page 36: I os 2

Declared Property

• Weak = Assign

• OwnerShip 없음, 언제 해제될 지 알 수 없음

• ARC, NON-ARC 모두 카운트 신경 X

Page 37: I os 2

Declared Property

• Copy

• Object Copy로 구현됨

Page 38: I os 2

Declared Property

• nonatomic

• 함수의 멀티 쓰레드 보장 하지 않음

• @synchronized()

!

• getter = SEL , setter = SEL

Page 39: I os 2

Declared Property

• readonly

• Getter만 구현

!

• @synthesize Property1

• @synthesize Property2 = instance Var

• 프로퍼티에 대응되는 변수 설정 가능

• 프로퍼티와 대응되는 변수가 없을 시에 컴파일러가 자동 생성

Page 40: I os 2

Declared Property

• dot ( . ) 연산자

• obj.property = val // [obj setProperty: val]

• val = obj.property // val = [obj property]

Page 41: I os 2

Declared Property

• dot ( . ) 연산자

• obj.property = val // [obj setProperty: val]

• val = obj.property // val = [obj property]

Page 42: I os 2

Protocol

Page 43: I os 2

Protocol

• 메서드 집합

!

• 상속관계 X

!

• 메서드 구현 책임

Page 44: I os 2

Protocol

• 선언부 - 헤더로 작성

• @procotol 프로토콜 이름

• 메서드 선언;

• @end

!

• @optional, @required

Page 45: I os 2

Protocol

• @intefacae 클래스:슈퍼클래스<프로토콜명>

Page 46: I os 2

Protocol

• 선언된 protocol의 메서드를 구현

Page 47: I os 2

Protocol

• @required로 선언된 메서드를 구현하지 않으면 Warning이 생김

Page 48: I os 2

Protocol

• Protocol은 상속 관계를 따른다

• 타입에도 선언이 가능하다

• ex) id <Protocol> obj;

Page 49: I os 2

Protocol

• Delegate 로 활용!

Page 50: I os 2

iOS

Page 51: I os 2

Delegate

Page 52: I os 2

Delegate

• Protocol 활용

!

• UIApplicationDelegate

• UIAlertViewDelegate

Page 53: I os 2

Delegate

• Data Source?

Page 54: I os 2

UIAlertView

Page 55: I os 2

UIAlertView

• 알림창 !

• Dialog

!

• 버튼 생성!

Page 56: I os 2

UIAlertView

Page 57: I os 2
Page 58: I os 2

• Button 이 2개 이상일 때 세로로 배열!

UIAlertView

Page 59: I os 2

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView !!-(void)willPresentAlertView:(UIAlertView *)alertView !-(void)didPresentAlertView:(UIAlertView *)alertView !-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex !-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex !!-(void)alertViewCancel:(UIAlertView *)alertView

UIAlertViewDelegate

Page 60: I os 2

UIAlertViewDelegate

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { //AlertView의 첫번째 Other Button을 활성화 할 것인지에 대한 여부 NSLog(@"alertViewShouldEnableFirstOtherButton"); return NO; }

Page 61: I os 2

UIAlertViewDelegate

!-(void)willPresentAlertView:(UIAlertView *)alertView { //AlertView가 화면에 보이기 전에 호출되는 메서드 NSLog(@"willPresentAlertView"); } !-(void)didPresentAlertView:(UIAlertView *)alertView { //AlertView가 화면에 보인 후에 호출되는 메서드 NSLog(@"didPresentAlertView"); }

Page 62: I os 2

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //버튼이 눌렸을 경우에 호출되는 메서드 NSLog(@"clickedButtonAtIndex"); NSString *message = [NSString stringWithFormat:@"Self \nButtonIndex : %d", buttonIndex]; [[[UIAlertView alloc] initWithTitle:@"Title" message:message delegate:alertViewDelegateTest cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; }

UIAlertViewDelegate

Page 63: I os 2

UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { //AlertView가 화면에서 사라지기 전에 호출되는 메서드 NSLog(@"willDismissWithButtonIndex"); } !-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { //AlertView가 화면에서 사라진 후에 호출되는 메서드 NSLog(@"didDismissWithButtonIndex"); }

Page 64: I os 2

UIAlertViewDelegate

2013-11-26 09:40:33.310 UITestExample[1831:70b] willPresentAlertView 2013-11-26 09:40:33.311 UITestExample[1831:70b] alertViewShouldEnableFirstOtherButton 2013-11-26 09:40:33.888 UITestExample[1831:70b] didPresentAlertView 2013-11-26 09:40:34.399 UITestExample[1831:70b] clickedButtonAtIndex 2013-11-26 09:40:34.407 UITestExample[1831:70b] willDismissWithButtonIndex 2013-11-26 09:40:34.407 UITestExample[1831:70b] didDismissWithButtonIndex

Page 65: I os 2

UIAlertViewDelegate

-(void)alertViewCancel:(UIAlertView *)alertView { //AlertView를 System에서 Dismiss한 경우에 불리는 메서드 //User의 Cancel Btn Click하고는 연관이 없다. NSLog(@"alertViewCancel"); }