ios 从不兼容的类型“ViewController *const_strong”分配给“id<Delegate>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9861538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:31:37  来源:igfitidea点击:

Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_strong'

iosobjective-cdelegates

提问by David L

Throughout my app, I'm getting semantic issue warnings when I set ViewController.delegate = self. I have searched and found similar posts but none were able to solve my problem.

在我的整个应用程序中,当我设置ViewController.delegate = self. 我搜索并找到了类似的帖子,但没有一个能够解决我的问题。

ViewController.m:

视图控制器.m:

GameAddViewController *gameAddViewContoller = [[navigationController viewControllers] objectAtIndex:0];
gameAddViewContoller.delegate=self;

I get the error message when setting .delegate=self.

我在设置时收到错误消息.delegate=self

GameAddViewController.h:

游戏添加视图控制器.h:

@protocol GameAddViewControllerDelegate <NSObject>

- (void)gameAddViewControllerDidCancel:(GameAddViewController *)controller;
- (void)gameAddViewController:(GameAddViewController *)controller didAddGame:(Game *) game;

@end

@interface GameAddViewController : UITableViewController <GameAddViewControllerDelegate>
{
sqlite3         *pitchcountDB;
NSString        *dbPath;
}
@property (nonatomic, strong) id <GameAddViewControllerDelegate> delegate;
...
@end

ViewController.h:

视图控制器.h:

#import "GameAddViewController.h"

@class ViewController;
@protocol ViewControllerDelegate <NSObject>
- (void)ViewControllerDidCancel:(ViewController *)controller;

@end
@interface ViewController : UIViewController <ViewControllerDelegate>
-(void) checkAndCreateFile;
@end

Can anyone point me in the right direction to resolve the warning messages?

任何人都可以指出我解决警告消息的正确方向吗?

回答by giorashc

At this line :

在这一行:

gameAddViewContoller.delegate=self; 

Notice that self is of type ViewControllerwhich does NOT conform to the GameAddViewControllerprotocol.

请注意 self 是ViewController不符合GameAddViewController协议的类型。

回答by Raul Lopez

For me what ended up happening is that I wasn't adding the delegate to the @interface on my header file

对我来说,最终发生的事情是我没有将委托添加到头文件中的 @interface

For example

例如

@interface TheNameOfYourClass : UIViewController <TheDelegatorClassDelegate>

@end

回答by borrrden

You are putting the < GameAddViewControllerDelegate > in the wrong place. It doesn't go on GameAddViewController, it goes on ViewController.

您将 < GameAddViewControllerDelegate > 放在错误的位置。它不在 GameAddViewController 上,而是在 ViewController 上。

回答by Custom Bonbons

This might help other people who are adding Multipeer Connectivity straight to a ViewController. At the top of myViewControllerName.h add '<MCSessionDelegate>':

这可能会帮助其他直接将 Multipeer Connectivity 添加到 ViewController 的人。在 myViewControllerName.h 的顶部添加“<MCSessionDelegate>”:

@interface myViewControllerName : UIViewController<MCSessionDelegate>

回答by Michael Tangs

also, if you define your delegate on xx.m, but you use it in other class. you may get this problem. so, just put protocol define on xx.h, when it is needed.

此外,如果您在 xx.m 上定义您的委托,但您在其他类中使用它。你可能会遇到这个问题。因此,只需在需要时将协议定义放在 xx.h 上。

回答by oskarko

On hybrid projects you should add your delegates on .h file instead of .m file

在混合项目中,您应该在 .h 文件而不是 .m 文件中添加您的委托

回答by bshirley

If you have a hybrid project, the protocol in Swift and the assignment in Objective-C:

如果你有一个混合项目,Swift 中的协议和 Objective-C 中的赋值:

Swift declaration:

斯威夫特声明:

protocol BackgroundTasking {
    func beginTask(withName: String, expirationHandler handler: (()->Void)?)
    func endTask(withName: String)
}

Objective-C assignment:

Objective-C 作业:

@property (nonatomic) id<BackgroundTasking> backgroundTasker;

_backgroundTasker = [[BackgroundTasker alloc] init]; // WARNING

Assigning to '__strong id' from incompatible type 'BackgroundTasker *'

从不兼容的类型“BackgroundTasker *”分配给“__strong id”

You need to declare the class (to remove this warning) and the functions (to make them accessible) as @objcfor them to be correctly bridged.

您需要将类(以删除此警告)和函数(以使其可访问)声明为@objc,以便正确桥接它们。

Correct Swift declaration:

正确的 Swift 声明:

@objc protocol BackgroundTasking {
    @objc func beginTask(withName: String, expirationHandler handler: (()->Void)?)
    @objc func endTask(withName: String)
}