89

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?

Piper
  • 1,266
  • 3
  • 15
  • 26
David L
  • 4,347
  • 3
  • 18
  • 30

7 Answers7

82

At this line :

gameAddViewContoller.delegate=self; 

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

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • 1
    What do I need to do to fix this. Is there something I need to change in the GameAddViewController protocol? – David L Mar 25 '12 at 21:30
  • Well, it depends on who you want to be your delegate. Who is implementing the GameAddViewController protocol – giorashc Mar 26 '12 at 07:39
  • GameAddViewController is the child of ViewController and I am implementing the gameAddViewControllerDidCancel method in ViewController. The error message is from the prepareForSegue method in ViewController when I set gameAddViewContoller.delegate=self. – David L Mar 26 '12 at 23:51
  • 2
    So do as @borrrden suggested. Conform the `ViewController` to the `GameAddViewControllerDelegate` delegate. – giorashc Mar 27 '12 at 07:25
  • 37
    OK, so I finally got it. I added this to my header file //#import "GameAddViewController.h" @interface ViewController : UIViewController @end// – David L Mar 28 '12 at 04:25
  • 3
    Or, this might help other people with similar problems: @interface myViewControllerName : UIViewController – Custom Bonbons Jul 28 '14 at 12:16
  • 3
    How did this get accepted and 72 votes? This answer just rewords XCode's warning and provides no solution. – SimpleJ Aug 08 '18 at 19:03
66

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
Raul Lopez
  • 751
  • 5
  • 7
14

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

borrrden
  • 33,256
  • 8
  • 74
  • 109
4

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>
Custom Bonbons
  • 1,609
  • 2
  • 12
  • 17
1

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.

1

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)
}
bshirley
  • 8,217
  • 1
  • 37
  • 43
0

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

oskarko
  • 3,382
  • 1
  • 26
  • 26