开发者

How to explain this Objective C method declaration "- method:parameter;"

开发者 https://www.devze.com 2023-03-11 06:01 出处:网络
Declaring, implementing and using method like this: Test.h: - method:parameter; Test.m: - 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消