Skip to content

Instantly share code, notes, and snippets.

@alaborie
Created July 12, 2012 21:01
Show Gist options
  • Save alaborie/3100972 to your computer and use it in GitHub Desktop.
Save alaborie/3100972 to your computer and use it in GitHub Desktop.
Clang 3.1: interesting warning...
@protocol MyProtocol <NSObject>
@end
int main(int argc, char *argv[])
{
@autoreleasepool
{
id <MyProtocol> foo = nil;
__unused id value = [foo valueForKey:@"bar"];
return EXIT_SUCCESS;
}
}
@alaborie
Copy link
Author

The protocol that I have wrote does not declare valueForKey:, but this method should still be accessible through the category NSKeyValueCoding on NSObject. The following code does not generate warning:

id foo = nil;
id value = [foo valueForKey:@"bar"];

This one does:

id < MyProtocol > foo = nil;
id value = [foo valueForKey:@"bar"];

It is as if the compiler does not consider the type 'id < MyProtocol >' of type 'id'. Which is really odd. And so if you force the cast of foo into an id, the code compile without any warning.

id < MyProtocol > foo = nil;
id value = [(id)foo valueForKey:@"bar"];

@alaborie
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment