【问题标题】:Assigning to 'NSObject<PushNotificationDelegate> *' from incompatible type 'AppDelegate *const __strong'从不兼容的类型“AppDelegate *const __strong”分配给“NSObject<PushNotificationDelegate> *”
【发布时间】:2018-08-08 00:19:47
【问题描述】:

我正在尝试在我的游戏中实现 Pushwoosh,它非常简单,但我在这里遇到了这个问题:

【问题讨论】:

    标签: ios objective-c xcode push-notification pushwoosh


    【解决方案1】:

    您的 AppDelegate 实现应如下所示:

    @implementation AppDelegate <PushNotificationDelegate>
    

    在第 20 行。

    这意味着您的 AppDelegate 符合PushNotificationDelegate 协议。

    【讨论】:

      【解决方案2】:

      阅读协议。基本上,协议是对象必须(或可能,在@optional 属性的情况下)具有的方法和/或属性的列表。您将该错误消息中的NSObject&lt;PushNotificationDelegate&gt; 读作“任何声明它实现PushNotificationDelegate 协议中的方法的NSObject 子类”。

      要声明您的类符合协议,请在其@interface@implementation 行之一的末尾写上&lt;&gt; 之间的协议名称。

      编译器会分别读取每个源文件,以及来自该源文件的 #import 的所有标头(如果您想了解更多信息,请阅读“编译单元”)。所以如果你在.m文件中写入&lt;PushNotificationDelegate&gt;位,只有.m文件中的代码知道,因为其他.m文件只能看到你在标题中写的内容。

      在您的情况下,AppDelegate.m 源文件应该看到这一点,但也许您有另一个源文件,您在其中设置了相同类型的委托,该委托仅包含 AppDelegate 的标头和所以看不到?

      无论如何,如果您了解此错误消息,您会看到PushNotificationManager.delegate 被声明为NSObject&lt;PushNotificationDelegate&gt;,这就是您的AppDelegate 必须能够将其分配给该属性。并且错误正确地表明AppDelegate 可能是NSObject,但不是PushNotificationDelegate

      声明您的类符合协议的好处是,如果您忘记实现所需的方法,编译器会打印一条错误消息。

      【讨论】:

        【解决方案3】:

        就我而言,我需要在头文件中遵守协议以使警告静音。我不知道我在做什么。

        #import <UIKit/UIKit.h>
        #import "BaseAppDelegate.h"
        #import <Pushwoosh/PushNotificationManager.h>
        
        @interface ChildAppDelegate : BaseAppDelegate <UIApplicationDelegate, PushNotificationDelegate>
        
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多