Search code examples
iosobjective-cxcodepush-notificationpushwoosh

Assigning to 'NSObject<PushNotificationDelegate> *' from incompatible type 'AppDelegate *const __strong'


Im trying to implement Pushwoosh into my game its very simple guide but I'm running into this issue here:

enter image description here


Solution

  • Read up on protocols. Basically, a protocol is a list of methods and/or properties that an object must (or may, in the case of @optional properties) have. You read NSObject<PushNotificationDelegate> in that error message as "any NSObject subclass that declares that it implements the methods in the PushNotificationDelegate protocol".

    To declare your class conforms to a protocol, you write the name of the protocol(s) in between < and > at the end of one of its @interface or @implementation lines.

    The compiler reads each source file separately, and all the headers you #import from that source file (read up on "compilation units" if you want to learn more). So if you write the <PushNotificationDelegate> bit in the .m file, only code in the .m file knows about it, because other .m files only see what you wrote in the header.

    In your case, the AppDelegate.m source file should see that, but maybe you have another source file in which you set the same type of delegate that only includes the header for AppDelegate and thus can't see it?

    Anyway, if you read this error message with this knowledge, you'll see that PushNotificationManager.delegate is declared as NSObject<PushNotificationDelegate>, and that's what your AppDelegate has to be to be able to assign it to that property. And the error correctly says that an AppDelegate may be an NSObject, but not a PushNotificationDelegate.

    The advantage of declaring that your class conforms to a protocol is that the compiler will print an error message if you forget to implement a required method.