Compiler warning “not found in protocol(s)” when using [[[UIApplication sharedApplication] delegate] myClass Property]?

StackOverflow https://stackoverflow.com/questions/855456

  •  21-08-2019
  •  | 
  •  

Question

I have myClass instantiated by my appDelegate, I have another class, newClass instantiated by myClass. From the newClass instance, I want to access a property in the myClass instance that created it. I have done this by:

[[[UIApplication sharedApplication].delegate myClass] property]

This works, I can actually 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 has been correctly declared in the .h and .m files, it's properties have been set and it has been synthesized.

It compiles and runs and I can actually get the property's value.

Should I ignore the warning in Xcode?

Is there a better way to access the myClass instance's property?

Was it helpful?

Solution

Look at the documentation for -[UIApplication delegate]. Note that it has a return type of:

id <UIApplicationDelegate>

Therefore, all you know about the returned object is that it conforms to this protocol. It is nothing less than a leap of faith to then assume that the delegate responds to the -myClass message, and not one the compiler is willing to make.

You could hack it slightly to work like so:

[[(MyAppDelegateClass *)[[UIApplication sharedApplication] delegate] myClass] property]

But it would be bad practice. Instead, I suggest you make your application delegate a singleton so you can do:

[[[MyAppDelegateClass sharedApplicationDelegate] myClass] property]

OTHER TIPS

I think at compile time Xcode can't make the leap from properties and normal Objective-C. It can't find the property so will just assume you know what you are doing and fill in the blanks. At run-time things are obviously ok.

Try this instead:

[[[[UIApplication sharedApplication] delegate] myClass] property];

BTW so you don't have code like this littered throughout you own code I usually do a #define in the header of the app delegate:

#define APPDELEGATE (myAppDelegate *)[[UIApplication sharedApplication] delegate]

So then in the code I can just go:

[[APPDELEGATE myClass] property];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top