Tutorial by Examples

Defining a new protocol: @protocol NewProtocol - (void)protocolMethod:(id)argument; - (id)anotherMethod; @end
By default, all the methods declared in a protocol are required. This means that any class that conforms to this protocol must implement those methods. It is also possible to declare optional methods. These method can be implemented only if needed. You mark optional methods with the @optional dire...
The following syntax indicate that a class adopts a protocol, using angle brackets. @interface NewClass : NSObject <NewProtocol> ... @end This means that any instance of NewClass will respond to methods declared in its interface but also it will provide an implementation for all the requ...
It's possible to declare protocol name without methods: @protocol Person; use it your code (class definition, etc): @interface World : NSObject @property (strong, nonatomic) NSArray<id<some>> *employees; @end and later define protocol's method somewhere in your code: @protocol...
if ([object respondsToSelector:@selector(someOptionalMethodInProtocol:)]) { [object someOptionalMethodInProtocol:argument]; }
Returns a Boolean indicating if the class conform the protocol: [MyClass conformsToProtocol:@protocol(MyProtocol)];

Page 1 of 1