i os 2
DESCRIPTION
2번째 PPTTRANSCRIPT
iOS - 2Sanghun Han
EZNIX 솔루션개발 연구원
Obj-C - Memory Management, Property, Protocol iOS - UIAlertView, Delegate
Memory Management
Memory?
• C : 작성자가 직접 관리
!
• Obj-C : Reference-Count 방식을 사용
• Obj-C 1.0에 도입, Obj-C 2.0에서도 사용 가능
• ARC
C Style
• malloc, calloc, realloc …
• free
Objective-C
• alloc
• retain
• release
• autorelease
Objective-C
• alloc +1
• retain +1
• release -1
• autorelease -1
Objective-C
• alloc +1 할당하면서 레퍼런스 카운트를 증가
• retain +1 레퍼런스 카운트를 증가
• release -1 레퍼런스 카운트를 감소
• autorelease -1 다 쓰고 나서 레퍼런스 카운트를 감소
Reference - Counting
• alloc
• 객체를 생성, 카운트는 1
• Ownership 개념
Reference - Counting
• retain
• 객체의 카운트를 ++
• 객체의 Owner가 하나 증가함
Reference - Counting
• release
• 객체의 카운트를 --
• 객체의 Owner가 하나 감소함
Reference - Counting
• autorelease
• 자동 해제
• NSAutoreleasePool과 연계
Reference - Counting
Reference - Counting
Autorelease
• 자동 해제 풀 • NSAutoreleasePool이 존재하지 않을 경우에 autorelease 메세지를 날리면, 런타임 에러가 발생
• 어플리케이션 생성 시 풀 생성 - 프로그램 종료 시 풀 해제
• 메모리 관리에 의미 없음, 지역 풀 사용 필요성
• 이벤트 풀 • NSApplication 이벤트 루프 관리
• 이벤트로 동작하는 루프 처리에서는 NSApplication이 자동 해제 풀을 관리
• 즉, 풀 생성을 따로 하지 않아도 자동으로 지역 풀 사용이 이루어짐
Convenience Constructor
• -(instancetype)initWithString:(NSString *)string;
• 객체 초기화 인스턴스 메서드
!
• +(instancetype)stringWithString:(NSString *)string;
• autorelease된 객체 리턴
• [[[NSString alloc] initWithString:@“~~”] autorelease]
instancetype keyword?
instancetype
instancetype
instancetype
ARC ?
ARC
• Automatic - Reference - Counting
컴파일러가 책임진다!
ARC
• retain, release, dealloc, autorelease 등을 사용자가 명시적으로 호출하지 않는다
!
• 컴파일러가 짝을 맞추어서 자동으로 코드를 생성한다.
!
• Compile Time Memory Management
ARC
• C Structure 내부의 Object는 관리되지 않음
!
• @property 속성이 달라짐
• retain, assign, copy / weak, strong
ARC
• retain, release 등은 어떤 형태로 호출하는 방법도 모두 금지된다.
• @selector(retain), @selector(release) 등도 금지된다.
!
• dealloc 메소드를 오버라이드 한다면, 코드 상에서 [super dealloc] 은 호출하지 않아야 한다.
ARC
• autorelease?
• NSAutoreleasePool
!
!
• @autoleasepool 로 변경!
ARC
• C structure 에서 객체를 멤버로 가져서는 안된다.
ARC
• Core Foundation 객체들은 ARC 적용이 되지 않는다.
!
• CFRetain, CFRelease 기존처럼 호출
!
• __bridge, __bridge_retained, __bridge_transfer
ARC• __bridge - 일반 캐스팅, 현상 유지
!
• __bridge_retained - Obj-C -> CF
• ARC 해제
• CFBridgingRetain(id X)
• __bridge_transfer - CF -> Obj-C
• ARC 적용
• CFBridgingRelease(CFTypeRef X)
ARC
Property
Property
• 접근자 !
• retain / assign / copy /// strong / weak
Declared Property
• @property ( 속성 ) Type varName;
• 선언부에 선언
!
• @synthesize
• 구현부에 선언
Declared Property
• Strong = Retain
• OwnerShip 부여, Retain ++
Declared Property
• Weak = Assign
• OwnerShip 없음, 언제 해제될 지 알 수 없음
• ARC, NON-ARC 모두 카운트 신경 X
Declared Property
• Copy
• Object Copy로 구현됨
Declared Property
• nonatomic
• 함수의 멀티 쓰레드 보장 하지 않음
• @synchronized()
!
• getter = SEL , setter = SEL
Declared Property
• readonly
• Getter만 구현
!
• @synthesize Property1
• @synthesize Property2 = instance Var
• 프로퍼티에 대응되는 변수 설정 가능
• 프로퍼티와 대응되는 변수가 없을 시에 컴파일러가 자동 생성
Declared Property
• dot ( . ) 연산자
• obj.property = val // [obj setProperty: val]
• val = obj.property // val = [obj property]
Declared Property
• dot ( . ) 연산자
• obj.property = val // [obj setProperty: val]
• val = obj.property // val = [obj property]
Protocol
Protocol
• 메서드 집합
!
• 상속관계 X
!
• 메서드 구현 책임
Protocol
• 선언부 - 헤더로 작성
• @procotol 프로토콜 이름
• 메서드 선언;
• @end
!
• @optional, @required
Protocol
• @intefacae 클래스:슈퍼클래스<프로토콜명>
Protocol
• 선언된 protocol의 메서드를 구현
Protocol
• @required로 선언된 메서드를 구현하지 않으면 Warning이 생김
Protocol
• Protocol은 상속 관계를 따른다
• 타입에도 선언이 가능하다
• ex) id <Protocol> obj;
Protocol
• Delegate 로 활용!
iOS
Delegate
Delegate
• Protocol 활용
!
• UIApplicationDelegate
• UIAlertViewDelegate
Delegate
• Data Source?
UIAlertView
UIAlertView
• 알림창 !
• Dialog
!
• 버튼 생성!
UIAlertView
• Button 이 2개 이상일 때 세로로 배열!
UIAlertView
-(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
UIAlertViewDelegate
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { //AlertView의 첫번째 Other Button을 활성화 할 것인지에 대한 여부 NSLog(@"alertViewShouldEnableFirstOtherButton"); return NO; }
UIAlertViewDelegate
!-(void)willPresentAlertView:(UIAlertView *)alertView { //AlertView가 화면에 보이기 전에 호출되는 메서드 NSLog(@"willPresentAlertView"); } !-(void)didPresentAlertView:(UIAlertView *)alertView { //AlertView가 화면에 보인 후에 호출되는 메서드 NSLog(@"didPresentAlertView"); }
-(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
UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { //AlertView가 화면에서 사라지기 전에 호출되는 메서드 NSLog(@"willDismissWithButtonIndex"); } !-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { //AlertView가 화면에서 사라진 후에 호출되는 메서드 NSLog(@"didDismissWithButtonIndex"); }
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
UIAlertViewDelegate
-(void)alertViewCancel:(UIAlertView *)alertView { //AlertView를 System에서 Dismiss한 경우에 불리는 메서드 //User의 Cancel Btn Click하고는 연관이 없다. NSLog(@"alertViewCancel"); }