How to explain this Objective C method declaration "- method:parameter;"
Declaring, implementing and using method like this:
Test.h:
- method:parameter;
Test.m:
- method:parameter{
return nil;
}
Using:
[test method:anObject];
There is no return-type and parameter-type, but it works without an开发者_StackOverflow社区y warning or error. Can somebody explain it?
As the Objective-C Programming Language document states:
If a return or parameter type isn’t explicitly declared, it’s assumed to be the default type for methods and messages — an
id
.
So:
- method:parameter;
effectively means:
- (id)method:(id)parameter;
and, correspondingly:
- method:parameter{
return nil;
}
effectively means:
- (id)method:(id)parameter{
return nil;
}
From The Objective-C Programming Language:
If a return or parameter type isn’t explicitly declared, it’s assumed to be the default type for methods and messages—an
id
.
Default type in Obj-C is id
. So here the both the return and parameter is id
.
精彩评论