A protocol. あるプロトコル。
conformsToProtocol:
Availability
- iOS 2.0+
- iPadOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Technology
- Objective-C Runtime Objective-Cランタイム
Declaration 宣言
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
Parameters パラメータ
protocol
Return Value 戻り値
YES
if the target conforms to protocol
, otherwise NO
.
Discussion 解説
A class “conforms to” a protocol if it adopts the protocol or inherits from another class that adopts it. Classes adopt protocols by listing them within angle brackets after the interface declaration. For example, here My
adopts the (fictitious) Affiliation
and Normalization
protocols:
A class also conforms to any protocols included in the protocols it adopts or inherits. Protocols incorporate other protocols in the same way classes adopt them.
プロトコルは、クラスがそれらを採用するのと同じ方法で他のプロトコルを取り入れます。
For example, here the Affiliation
protocol incorporates the Joining
protocol:
If a class adopts a protocol that incorporates another protocol, it must also implement all the methods in the incorporated protocol or inherit those methods from a class that adopts it. あるクラスが別のプロトコルを取り入れるプロトコルを採用するならば、それは、取り入れられたプロトコル中の全てのメソッドを実装するか、それを採用するクラスからそれらのメソッドを継承しなければなりません。
This method determines conformance solely on the basis of the formal declarations in header files, as illustrated above. It doesn’t check to see whether the methods declared in the protocol are actually implemented—that’s the programmer’s responsibility. このメソッドは、ただ単にヘッダファイル中の形の上での宣言に基づいて準拠を、上で説明されるように、判定します。それは、プロトコルで宣言されるメソッドが実際に実装されるかどうか確認しません—それはプログラマーの責任です。
To specify the protocol required as this method’s argument, use the @protocol()
directive:
Performance Considerations
Calling this method in performance sensitive code can cause unwanted performance problems. conforms
requires taking the Objective-C runtime lock and traversing the target’s class hierarchy to check for protocol conformance, which can take significant time.
Consider the following alternatives in your code:
Use
responds
to check for methods in the protocol instead, especially if you only need to check some of the protocol’s methods.To Selector: If you do need to use
conforms
, cache the result whenever possible, rather than calling this method repeatedly.To Protocol: