从不兼容类型id分配给'AppDelegate *'

时间:2022-03-10 04:15:54

In my project it says Assigning to 'AppDelegate *' from incompatible type 'id'.

在我的项目中,它说从不兼容的类型“id”分配到“AppDelegate *”。

What exactly is this? Why did this warning occur?

这到底是什么?为什么会出现这种警告?

I have declared in .m

我已经声明了。

AppDelegate *appdev;

and in viewDidLoad

而在viewDidLoad

{
    appdev = [[UIApplication sharedApplication]delegate];     <= warning here
}

I want to hide this warning. What should I do? Thanks in advance.

我想隐藏这个警告。我应该做什么?提前谢谢。

4 个解决方案

#1


79  

since you know they equal, add a cast to let the compiler know

因为您知道它们是相等的,所以添加一个强制转换以让编译器知道

 AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication]delegate];

since this might come up in Swift too

因为这可能很快也会出现

 let app = UIApplication.shared.delegate as! AppDelegate

#2


7  

If you want you have this for import AppDelegate anywhere. Is simple.

如果你想要在任何地方导入AppDelegate。很简单。

IN APPDELEGATE

在APPDELEGATE

/**
 *  Get AppDelegate
 *  Call [AppDelegate getAppDelegate]
 *
 *  @return AppDelegate
 */
+ (AppDelegate *) app {
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

ANYWHERE IN YOUR CLASS

在你的班级

#import "AppDelegate.h" // TOP OF YOUR CLASS

AppDelegate *app = [AppDelegate app];

#3


3  

You can type-cast it to prevent the warning message.

您可以键入它来阻止警告消息。

try:

试一试:

appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];

#4


1  

You need to type cast because it returns Protocol.

您需要键入cast,因为它返回协议。

appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];

#1


79  

since you know they equal, add a cast to let the compiler know

因为您知道它们是相等的,所以添加一个强制转换以让编译器知道

 AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication]delegate];

since this might come up in Swift too

因为这可能很快也会出现

 let app = UIApplication.shared.delegate as! AppDelegate

#2


7  

If you want you have this for import AppDelegate anywhere. Is simple.

如果你想要在任何地方导入AppDelegate。很简单。

IN APPDELEGATE

在APPDELEGATE

/**
 *  Get AppDelegate
 *  Call [AppDelegate getAppDelegate]
 *
 *  @return AppDelegate
 */
+ (AppDelegate *) app {
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

ANYWHERE IN YOUR CLASS

在你的班级

#import "AppDelegate.h" // TOP OF YOUR CLASS

AppDelegate *app = [AppDelegate app];

#3


3  

You can type-cast it to prevent the warning message.

您可以键入它来阻止警告消息。

try:

试一试:

appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];

#4


1  

You need to type cast because it returns Protocol.

您需要键入cast,因为它返回协议。

appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];