Compiler warning "not found in protocols" when using [[[UIApplication sharedApplication] delegate] myClass Property]?
I have myClass created by my appDelegate, I have another class, newClass, created by myClass. From the newClass instance, I want to access a property on the myClass instance that created it. I did this:
[[[UIApplication sharedApplication].delegate myClass] property]
This works, I can get the property, but I get this warning from Xcode:
warning: "-myClass" not found in protocols
warning: no "-myClass" method found
(messages without a matching signature will be assumed to return "id" and accept "..." as arguments)
The newClass property was correctly declared in the .h and .m files, its properties were set and synthesized.
It compiles and runs and I can get the value of the property.
Should I ignore the warning in Xcode?
Is there a better way to access myClass instance property?
a source to share
See the documentation for - [UIApplication delegate]. Note that it has a return type:
id <UIApplicationDelegate>
Therefore, all you know about the returned object is that it conforms to this protocol. It is nothing more than a leap of faith to then assume that the delegate is responding to a message -myClass
, not a compiler that is willing to do.
You can hack it a bit like this:
[[(MyAppDelegateClass *)[[UIApplication sharedApplication] delegate] myClass] property]
But that would be bad practice. Instead, I suggest you make your application delegate one single so that you can:
[[[MyAppDelegateClass sharedApplicationDelegate] myClass] property]
a source to share
I think at compile time Xcode cannot jump out of properties and normal Objective-C. It can't find the property, so just assume you know what you are doing and fill in the blanks. At runtime, everything is clearly fine.
Try this instead:
[[[[UIApplication sharedApplication] delegate] myClass] property];
By the way, you don't have any code like this lost in all your own code. I usually do a #define in the app delegate header:
#define APPDELEGATE (myAppDelegate *)[[UIApplication sharedApplication] delegate]
So then, in code, I can just go:
[[APPDELEGATE myClass] property];
a source to share