Can I ignore this iphone warning?
-
01-10-2019 - |
Question
I have this code:
if([annotation respondsToSelector:@selector(tag)]){
disclosureButton.tag = [annotation tag];
}
and I get the warning:
'-tag' not found in protocol
Fair enough, but I've created a new object with the protocol that has a synthesized int tag
variable.
EDIT: found why the app was crashing - not this line. Now I just get a warning and the app works fine.
Thanks Tom
Solution
The warning is generated because for the static type of annotation
, MKAnnotation
, there is no method -tag
. As you already checked wether the dynamic type responds to the selector you can ignore the warning in this case.
To get rid of the warning:
If you expect a certain class you can test for it instead:
if ([annotation isKindOfClass:[TCPlaceMark class]]) { disclosureButton.tag = [(TCPlaceMark *)annotation tag]; }
For a protocol:
if ([annotation conformsToProtocol:@protocol(PlaceProtocol)]) { disclosureButton.tag = [(id<PlaceProtocol>)annotation tag]; }
Or if both do not apply use a specific protocol to suppress the warning (useful e.g. with rapidly changing Apple APIs):
@protocol TaggedProtocol - (int)tag; @end // ... if([annotation respondsToSelector:@selector(tag)]){ disclosureButton.tag = [(id<TaggedProtocol>)annotation tag]; }