Reputation: 1746
My iPhone app has a login view controller to pop up whenever it is necessary to login. After the user loged in, I have this:
if ([self.presentingViewController respondsToSelector:@selector(userDidLogin)]) {
[((id)self.presentingViewController) userDidLogin];
} else {
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
However, the compiler kept complaining about "No known instance method for selector userDidLogin". Then I added an instance method named userDidLogin
for the login view controller, which was of course not self.presentingViewController
, then the build succeeded.
This workaround feels unreasonable to me. Is it a bug in Xcode or intended behavior? Is it is the latter, what is the rationale?
Upvotes: 3
Views: 1839
Reputation: 385670
The compiler needs to know the return type of the userDidLogin
selector so that it can generate correct code:
objc_msgSend_stret
. (Source: Greg Parker's blog.)objc_msgSend_fpret
. (Source: Greg Parker's blog.)objc_msgSend
.The userDidLogin
selector has no arguments, but if the selector did have arguments, the compiler would also need to know the declared argument types so it could pass the arguments correctly.
Additionally, if you're using ARC, the compiler needs to know the return type and ownership annotations of the selector so that it can generate a release of the return value if appropriate.
The usual way to handle this is just to #import
the header file of the class that declares the userDidLogin
message. As long as the compiler has seen the selector declared somewhere, it won't complain about sending it to an id
.
Upvotes: 5