Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_strong'
IosObjective CDelegatesIos Problem Overview
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.m:
GameAddViewController *gameAddViewContoller = [[navigationController viewControllers] objectAtIndex:0];
gameAddViewContoller.delegate=self;
I get the error message when setting .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
Can anyone point me in the right direction to resolve the warning messages?
Ios Solutions
Solution 1 - Ios
At this line :
gameAddViewContoller.delegate=self;
Notice that self is of type ViewController
which does NOT conform to the GameAddViewController
protocol.
Solution 2 - Ios
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
Solution 3 - Ios
You are putting the < GameAddViewControllerDelegate > in the wrong place. It doesn't go on GameAddViewController, it goes on ViewController.
Solution 4 - Ios
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>
Solution 5 - Ios
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.
Solution 6 - Ios
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
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)
}
Solution 7 - Ios
On hybrid projects you should add your delegates on .h file instead of .m file