ݺߣ

ݺߣShare a Scribd company logo
iOS - 2
Sanghun 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
I os 2
UIAlertView

• Button 이 2개 이상일 때 세로로 배열!
UIAlertViewDelegate
-(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

-(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");
}
UIAlertViewDelegate

-(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
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:
(NSInteger)buttonIndex
{
//AlertView가 화면에서 사라지기 전에 호출되는 메서드
NSLog(@"willDismissWithButtonIndex");
}

!

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

2013-11-26
2013-11-26
2013-11-26
2013-11-26
2013-11-26
2013-11-26

09:40:33.310
09:40:33.311
09:40:33.888
09:40:34.399
09:40:34.407
09:40:34.407

UITestExample[1831:70b]
UITestExample[1831:70b]
UITestExample[1831:70b]
UITestExample[1831:70b]
UITestExample[1831:70b]
UITestExample[1831:70b]

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

More Related Content

What's hot (20)

PPTX
1. introduction to java8
흥래 김
PDF
Clojure/Chapter3
destinycs
PDF
Java lambda
Hyosang Hong
PDF
Realm.io for iOS
Eunjoo Im
PDF
Introduce php7
Jung soo Ahn
PPTX
Angular2 가기전 Type script소개
Dong Jun Kwon
PDF
[Swift] Properties
Bill Kim
PDF
Realm @Android
Theodore(Yongbin) Cha
PPTX
4-1. javascript
JinKyoungHeo
PPTX
Inheritance
JIhyun Choi
PDF
EcmaScript6(2015) Overview
yongwoo Jeon
PDF
[D2 campus seminar]스칼라를 통한 다양한 언어의 패러다임 맛보기
NAVER D2
PDF
C++ Advanced 강의 1주차
HyunJoon Park
PDF
Scala로의 산책
Youmi Bae
PPTX
ECMAScript 6의 새로운 것들!
WooYoung Cho
PPTX
17 swift 프로토콜
Changwon National University
PDF
Swift3 typecasting nested_type
Eunjoo Im
PPTX
SLiPP 서비스를 Java에서 Scala로 전환하면서 경험담
Javajigi Jaesung
PDF
Swift3 subscript inheritance initialization
Eunjoo Im
PDF
Effective Scala (SpringCamp 2013)
ihji
1. introduction to java8
흥래 김
Clojure/Chapter3
destinycs
Java lambda
Hyosang Hong
Realm.io for iOS
Eunjoo Im
Introduce php7
Jung soo Ahn
Angular2 가기전 Type script소개
Dong Jun Kwon
[Swift] Properties
Bill Kim
Realm @Android
Theodore(Yongbin) Cha
4-1. javascript
JinKyoungHeo
Inheritance
JIhyun Choi
EcmaScript6(2015) Overview
yongwoo Jeon
[D2 campus seminar]스칼라를 통한 다양한 언어의 패러다임 맛보기
NAVER D2
C++ Advanced 강의 1주차
HyunJoon Park
Scala로의 산책
Youmi Bae
ECMAScript 6의 새로운 것들!
WooYoung Cho
17 swift 프로토콜
Changwon National University
Swift3 typecasting nested_type
Eunjoo Im
SLiPP 서비스를 Java에서 Scala로 전환하면서 경험담
Javajigi Jaesung
Swift3 subscript inheritance initialization
Eunjoo Im
Effective Scala (SpringCamp 2013)
ihji

Viewers also liked (16)

PDF
soshosai2012_pamphlet
a-gakusai
PPT
Future
jj17ns
PPTX
Cis 5200presentation groupb
Narendra Mali
PPTX
1st quiz 250
jj17ns
PPT
Changes of State Concerning Matter
KarenBJ25
PPT
Razdel1
necomusume
PPTX
iLearn
gavinsmart
PDF
Informe sobre-becas-presidenciales
William Alexander McCormick Rivera
PDF
Skala pengukuran dan teknik penskalaan
Nani Warnasari
PDF
I os 1
Sanghoon Han
PDF
MLearning Revolutionizes Education - Even Yawen Wu
Even Wu
PDF
Pnf2012
a-gakusai
PPT
Changes of state edu 653
KarenBJ25
PDF
Lesson 1 on matter
KarenBJ25
PPT
Unit review what´s your medicine
jj17ns
DOCX
Program khas murid orang asli
gemilangfaudzi
soshosai2012_pamphlet
a-gakusai
Future
jj17ns
Cis 5200presentation groupb
Narendra Mali
1st quiz 250
jj17ns
Changes of State Concerning Matter
KarenBJ25
Razdel1
necomusume
Informe sobre-becas-presidenciales
William Alexander McCormick Rivera
Skala pengukuran dan teknik penskalaan
Nani Warnasari
MLearning Revolutionizes Education - Even Yawen Wu
Even Wu
Pnf2012
a-gakusai
Changes of state edu 653
KarenBJ25
Lesson 1 on matter
KarenBJ25
Unit review what´s your medicine
jj17ns
Program khas murid orang asli
gemilangfaudzi
Ad

Similar to I os 2 (20)

PPT
I phone 2 release
Jaehyeuk Oh
PDF
Object C - RIP
Dong Heon Cho
PDF
[Swift] ARC
Bill Kim
PDF
iOS 메모리관리
Changwon National University
PDF
11스위트로그래밍언
NAVER D2
PDF
Objective-C Runtime Programming Guide
Sung-Kwan Kim
PPT
iOS 앱 개발 강의 자료 #1
Jeong-Hoon Mo
PPTX
Arc
정문 김
PDF
[Osxdev]4.swift
NAVER D2
PDF
스위프트 성능 이해하기
Yongha Yoo
PDF
[112] 실전 스위프트 프로그래밍
NAVER D2
KEY
아이폰 앱 패턴
조 용구
PDF
[SwiftStudy 2016] 2장. Swift 타입 파트 1
Keunhyun Oh
PDF
Rx for iOS App. RxMVVM-DataCenter!
SUNGCHEOL KIM
PDF
Game programming patterns 2
QooJuice
PDF
Smalltalk at Altlang 2008
daliot
PDF
Swift5 vs objective c
Bill Kim
PDF
[Swift] Closure
Bill Kim
PPTX
Golang Project Guide from A to Z: From Feature Development to Enterprise Appl...
Kyuhyun Byun
PPTX
Swift3 : class and struct(+property+method)
승욱 정
I phone 2 release
Jaehyeuk Oh
Object C - RIP
Dong Heon Cho
[Swift] ARC
Bill Kim
iOS 메모리관리
Changwon National University
11스위트로그래밍언
NAVER D2
Objective-C Runtime Programming Guide
Sung-Kwan Kim
iOS 앱 개발 강의 자료 #1
Jeong-Hoon Mo
[Osxdev]4.swift
NAVER D2
스위프트 성능 이해하기
Yongha Yoo
[112] 실전 스위프트 프로그래밍
NAVER D2
아이폰 앱 패턴
조 용구
[SwiftStudy 2016] 2장. Swift 타입 파트 1
Keunhyun Oh
Rx for iOS App. RxMVVM-DataCenter!
SUNGCHEOL KIM
Game programming patterns 2
QooJuice
Smalltalk at Altlang 2008
daliot
Swift5 vs objective c
Bill Kim
[Swift] Closure
Bill Kim
Golang Project Guide from A to Z: From Feature Development to Enterprise Appl...
Kyuhyun Byun
Swift3 : class and struct(+property+method)
승욱 정
Ad

I os 2