typeoneerror
typeoneerror

Reputation: 56968

Semantic issue with unrelated object in Objective-C

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?

Upvotes: 4

Views: 411

Answers (2)

Alexsander Akers
Alexsander Akers

Reputation: 16024

Have you tried directly casting the result of -alloc? Something like this:

ProfileViewController *profileViewController = [(ProfileViewController *)[ProfileViewController alloc] initWithUser:friend];

Upvotes: 1

PengOne
PengOne

Reputation: 48398

The problem is you have an ambiguous selector. Because alloc returns the generic (for Objective C) type id, the call to initWithUser: has become ambiguous, and so it gets confused with the NSUserDefaults method initWithUser:. The compiler thinks you're trying to use that one.

You can remove the ambiguity by casting:

ProfileViewController *profileViewController = [(ProfileViewController*)[ProfileViewController alloc] initWithUser:friend];

Upvotes: 2

Related Questions