Cast an instance of a class to a @protocol in Objective-C


Cast an instance of a class to a @protocol in Objective-C



I have an object (a UIViewController) which may or may not conform to a protocol I've defined.

I know I can determine if the object conforms to the protocol, then safely call the method:

if([self.myViewController conformsToProtocol:@protocol(MyProtocol)]) {     [self.myViewController protocolMethod]; // <-- warning here } 

However, XCode shows a warning:

warning 'UIViewController' may not respond to '-protocolMethod' 

What's the right way to prevent this warning? I can't seem to cast self.myViewController as a MyProtocol class.




Objective C: Modulo bias

1:



UIScrollview with UIButtons - how to recreate springboard?
The correct way to do this is to do:.
Why do Objective C files use the .m extension?
if ([self.myViewController conformsToProtocol:@protocol(MyProtocol)]) {         UIViewController <MyProtocol> *vc = (UIViewController <MyProtocol> *) self.myViewController;         [vc protocolMethod]; } 
The UIViewController <MyProtocol> * type-cast translates to "vc is a UIViewController object that conforms to MyProtocol", whereas using id <MyProtocol> translates to "vc is an object of an unknown class that conforms to MyProtocol"..
How to Convert NSInteger to a binary (string) value
This way you the compiler will give you proper type checking on vc - the compiler will only give you a warning if any method that's not declared on either UIViewController or <MyProtocol> is called.


Why has NSNumber such strange retainCounts?
id should only be used in this situation if you don't know the class/type of the object being cast..
Modulo with long long integers in Objective C


How does a Protocol on an instance variable in Objective-C work?


NSMutableArray as @property with readonly

2:


You can cast it like this:.
if([self.myViewController conformsToProtocol:@protocol(MyProtocol)]) {     id<MyProtocol> p = (id<MyProtocol>)self.myViewController;     [p protocolMethod]; } 
This threw me for a bit, too.

In Objective-C, the protocol isn't the type itself, so you need to specify id (or some other type, such as NSObject) along with the protocol that you want..



82 out of 100 based on 42 user ratings 1342 reviews