u 프로토콜
n 정의
Protocols는 클래스 정의와 관련 없는 mothod선언 목록.
Protocols는 정의되어 있지 않은 객체에 메시지를 전달하고자 할 때 사용 가능
n 사용 범위
- 다른 사람들이 구현해야 할 mothod를 선언할 때
- 클래스는 숨긴 상태에서 object의 interface을 선어할 때
- 계층적으로 관련없는 클래스 간에 유사성을 가지게 할 때
n Declaring Interfaces for Others to Implement
프로토콜로 선언된 메소드를 사용하려면, 프로토콜을 adopt(클래스가 프로토콜의 모든 메소드를 구현한다는 의미)하고, 그 메소드들을 구현.
- (void)mouseDown:(NSEvent *)theEvent; - (void)mouseDragged:(NSEvent *)theEvent; - (void)mouseUp:(NSEvent *)theEvent; |
n Methods for Others to Implement
- setAssistant:anObject { assistant = anObject; } |
n Declaring Interfaces for Anonymous Objects
id formatter = [receiver formattingService]; |
n Non-Hierarchical Similarities
- (NSXMLElement *)XMLRepresentation; - initFromXMLRepresentation:(NSXMLElement *)xmlString; |
n Formal Protocols (선언 방법)
@protocol 지시자를 사용
@protocol ProtocolName method declarations @end |
n XML을 이용한 protocol 표현이 가능
@protocol MyXMLSupport - initFromXMLRepresentation:(NSXMLElement *)XMLElement; - (NSXMLElement *)XMLRepresentation; @end |
n 주의점
- protocol은 클래스랑 연관지어 쓰는 것으로 단독으로 사용되지 않는다.
따라서, protocol 이름은 자체의 namespace안에만 있게 되어 global하게 접근할 수 없다.
- protocol을 사용하려면 class가 그 protocol을 adopt해야 한다.
- protocol을 adopt할 클래스에서는 protocol이 선언된 헤더파일을 import 해야 한다.
n Informal Protocols(Category 선언 내에 method를 선언한 것)
@interface NSObject ( MyXMLSupport ) - initFromXMLRepresentation:(NSXMLElement *)XMLElement; - (NSXMLElement *)XMLRepresentation; @end |
특징
- informal protocol은 category와 선언방식은 똑같다.
차이점은 Catogory는 구현을 따로 카테고리로 만들어서 해야 하는 반면,
informal protocol은 구현을 클래스의method로 해 준다
n Protocol Objects
런타임시 클래스는 class object로 method는 selector로 formal protocol은 protocol class로
protocol object는 @protocol(protocol_name)지시자로 참조할 수 있다.
Protocol *myXMLSupportProtocol = @protocol(MyXMLSupport); |
n Adoption a Protocol(adopt는 클래스가 protocol의 모든 mothod를 구현한다는 의미)
@interface ClassName : ItsSuperclass < protocol list > @interface ClassName ( CategoryName ) < protocol list > @interface Formatter : NSObject < Formatting, Prettifying > |
n Conforming to a Protocol
-클래스가 protocol을 adopt하고 있는지 알아보기 위해 conformsToProtocol:메시지를 object에 보낸다.
-respondsToSelector:는 한 method이 구현되어 있는지를 알려준다.
-isKindOfClass:는 클래스가 상속 계층구조안에 속하는지 여부를 알려준다.
if ( ! [receiver conformsToProtocol:@protocol(MyXMLSupport)] ) { // Object does not conform to MyXMLSupport protocol // If you are expecting receiver to implement methods declared in the // MyXMLSupport protocol, this is probably an error } |
n Type Checking
protocol 타입선언 방법은 “type_name <protocol_name> 변수” 형태이다.
- (id <Formatting>)formattingService; id <MyXMLSupport> anObject; Formatter *anObject; id <Formatting> anObject; |
n Protocols Within Protocols
@protocol ProtocolName < protocol list > @protocol Paging < Formatting > id <Paging> someObject; if ( [anotherObject conformsToProtocol:@protocol(Paging)] ) |
n 상호참조 문제 해결
// A 선언 #import "B.h" @protocol A - foo:(id <B>)anObject; @end // B 선언 #import "A.h" @protocol B - bar:(id <A>)anObject; @end // A, B protocol이 상호 참조 @protocol B; @protocol A - foo:(id <B>)anObject; @end |
'아이폰개발' 카테고리의 다른 글
[아이폰개발] Objective-C 5.카테고리와 확장 (0) | 2010.12.20 |
---|---|
[아이폰개발] Objective-C 4.프로퍼티 (0) | 2010.12.17 |
[아이폰개발] Objective-C 2.오브젝트의 메모리 할당과 초기화 (0) | 2010.12.15 |
[아이폰개발] Objective-C 1.클래스 정의 (0) | 2010.12.15 |
[아이폰개발] 오브젝트C 한글 레퍼런스 (2) | 2010.12.08 |