Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

< 前ページ次ページ >

正式なプロトコル

Objective-C言語では、メソッドのリストをプロトコルとして形式的に宣言する方法を提供しています。形式プロトコルは、言語とランタイムシステムによってサポートされます。たとえば、コンパイラはプロトコルに基づいて型をチェックすることができ、オブジェクトはプロトコルに準拠しているかどうかを実行時にイントロスペクションを実行して報告することができます。

このセクションの内容:

プロトコルの宣言
任意のプロトコルメソッド


プロトコルの宣言

形式プロトコルは、@protocolディレクティブを使って宣言します。

@protocol ProtocolName
method declarations
@end

たとえば、次のようなXML表現プロトコルを宣言できます。

@protocol MyXMLSupport
- (NSXMLElement *)XMLRepresentation;
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
@end

クラス名と異なり、プロトコル名にはグローバルな可視性がありません。プロトコル名は自身のネームスペースに属します。

任意のプロトコルメソッド

プロトコルメソッドは、@optionalキーワードを使用してオプションとして指定できます。@optionalモーダルキーワードに対応して、デフォルトの振る舞いのセマンティクスを正式に示す@requiredキーワードがあります。@optional@requiredを使用して、プロトコルを適切と思われるセクションに分割できます。キーワードを指定しない場合のデフォルトは、@requiredです。

@protocol MyProtocol
 
- (void)requiredMethod;
 
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
 
@required
- (void)anotherRequiredMethod;
 
@end


< 前ページ次ページ >


Last updated: 2007-10-31




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice