开发者

Isn't objective-c function parameter syntax weird? [closed]

开发者 https://www.devze.com 2023-02-16 06:59 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect 开发者_如何转开发answers to be supported by facts, references,or expertise, but this question will likely
As it currently stands, this question is not a good fit for our Q&A format. We expect 开发者_如何转开发answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Consider the following method:

+(void) myMethod:(int)arg1 **argument2**(int)arg2 **argument3**(int) arg3;

See how the first argument, unlike the 2nd and 3rd, doesn't have a description, giving it an impression of bad symmetry. Also you would expect the extra typing will provide named argument as you pass it in, but you still have to pass them in the correct order.

Can anyone help me make sense of this?


You're missing : after argument2 and argument3

Also, first argument is named myMethod. By Apple's naming recommendation guide, you'd see the method should be named in the manner that identifies semantics of first argument.

EDIT:

check out this document Coding Guidelines - Naming Methods


Hopefully the response to this other question will help you make sense of what you see.


The logic behind this exists though hard to get used to.
regarding your first note, about the naming of the first param, Apple encourage you to name your methods as follows:

+(void)myMethodWithArg1:(int)arg1 Arg2:(int)arg2 Arg3:(int)arg3;

thus making the name readable like a sentence in english
(my method had Arg1 of type int, Arg2 of type int, etc)

regarding the named params and the inability to change the order, that makes no sence to me either

and the comment above me is correct, you are missing those annoying : after the params

In addition, the syntax of ObjC has strong relation to that of Smalltalk (http://www.smalltalk.org/main/)
I'd encourage you to read on that and the relation between the two languages

hope this helps


The method name is supposed to described the first argument.

Like:

+ (void)updateUserWithId:id andAge:age

So that the whole expression gives sort of a natural sentence.

0

精彩评论

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