Xcode“AppDelegate * const __strong”参数的不兼容types“错误

当我声明appDelegate接口如下,以设置NSXMLParserDelegate ,我收到来自其他视图使用[[UIApplication sharedApplication]委托]的一些警告;

 @interface AppDelegate : UIResponder <UIApplicationDelegate, NSXMLParserDelegate> 

警告:用不兼容的types“id”expression式初始化“AppDelegate * __ strong”

但是,如果我删除它,由于xmlParser的自我设置,会出现另一个警告,

 @interface AppDelegate : UIResponder <UIApplicationDelegate> 

警告:将“AppDelegate * const __strong”发送给不兼容types“id”的参数

  xmlParser = [[NSXMLParser alloc] initWithData:receivedData]; [xmlParser setDelegate:self]; 

如何删除两个? 谢谢

你真的不应该让你的AppDelegate公开暴露接口。 它会在你所有的代码之间创build非常紧密的耦合 如果其他代码(在你的AppDelegate之外)需要一个NSXMLParserDelegate,你应该为它做一个不同的类。

它看起来像你的AppDelegate需要成为一个委托为自己的目的。 你可以通过在你的AppDelegate.m文件中创build一个类扩展来“私下”实现这个接口。

 @interface AppDelegate() <NSXMLParserDelegate> @end 

这样做会消除您在此收到的警告:

 [xmlParser setDelegate:self];