从不兼容的类型‘ ViewController * const_strong’赋值给‘ id < Committee >’

在整个应用程序中,当我设置 ViewController.delegate = self时,我会收到语义问题警告。我已经搜索和发现类似的帖子,但没有一个能够解决我的问题。

ViewController.m:

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

当设置 .delegate=self时,我得到错误消息。

返回文章页面 GameAddViewController.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:

#import "GameAddViewController.h"


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


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

有人能给我指出正确的方向来解决这些警告信息吗?

96224 次浏览

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

At this line :

gameAddViewContoller.delegate=self;

Notice that self is of type ViewController which does NOT conform to the GameAddViewController protocol.

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

For example

@interface TheNameOfYourClass : UIViewController <TheDelegatorClassDelegate>


@end

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

@interface myViewControllerName : UIViewController<MCSessionDelegate>

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.

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

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

Swift declaration:

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

Objective-C assignment:

@property (nonatomic) id<BackgroundTasking> backgroundTasker;


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

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

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

Correct Swift declaration:

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