I have a UIViewController subclass that implements a message to init the controller with a custom model:
- (id)initWithUser:(FacebookFriend *)user;
When I use this to init my controller:
ProfileViewController *profileViewController = [[ProfileViewController alloc] initWithUser:friend];
The compiler complains about sending a message to NSUserDefaults‘ message of the same name:
- (id)initWithUser:(NSString *)username;
warning: incompatible Objective-C types 'struct FacebookFriend *', expected 'struct NSString *' when passing argument 1 of 'initWithUser:' from distinct Objective-C type
I don’t quite understand why it’s notifying me of this as I don’t think UIViewController inherits from NSUserDefaults anywhere. Is there a way to disable this error? Could this cause a problem? Should I just rename my class’ initializer to avoid confusion?
The problem is you have an ambiguous selector. Because
allocreturns the generic (for Objective C) typeid, the call toinitWithUser:has become ambiguous, and so it gets confused with theNSUserDefaultsmethodinitWithUser:. The compiler thinks you’re trying to use that one.You can remove the ambiguity by casting: