0

Most languages I know use following syntax:

function name (var1,var2,var3) {
    //do stuff with variables
}

Objective-C however makes somewhat more complicated structure

- (function type) functionName:(type)var1 var2:(type) var2  var3:(type) var3{
}

Why simply not

- (function type) functionName:(type) var1 (type) var2 (type) var3{
}

Is it possible to do following, and what sense it would make?

- (function type) functionName:(type)var1 randomName:(type) var2  anotherName:(type) var3{
}
2
  • 1
    It's based on Smalltalk's message syntax; the labels are for readability. It's not really clear what the thing is that you're asking about the possibility of. Your last snippet is exactly the right syntax. Can you explain your example in more detail? Commented Feb 27, 2013 at 20:46
  • so randomName and anotherName are simply labels, used for explanatory purposes, and var2 and var3 are the actual variables, passed to the method, correct? Commented Feb 27, 2013 at 20:58

3 Answers 3

8

It is perfectly legal Objective-C to omit the part before the colon. If the API designers had wanted, they could have written:

- (void)removeObserver:(NSObject *)observer :(NSString *)keyPath :(void *)context

instead of

- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context

Now try reading that code

[self removeObserver:self :@"something" :@"somethingElse"];

as opposed to

[self removeObserver:self forKeyPath:@"something" context:@"somethingElse"];

What's more readable?

By the way, there are a handful of OS X API methods where method name and arguments are not interleaved. One example is CAMediaTimingFunction:

+ (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
Sign up to request clarification or add additional context in comments.

1 Comment

Technically, they aren't named arguments, but interleaved method name with arguments. (Named arguments leads to confusion with keyword arguments and/or the mistaken notion that parts of a method can be omitted at will).
3

Objective-C methods are designed to be self documenting, and they borrow from the rich tradition of Smalltalk. You can see a detailed explanation on this Stackoverflow answer

And by self documenting it means that the methods declaration itself gives you the information of what's the method main functionality and the purpose of the parameters been received.

So, to answer your last question, yes you can use a method declaration like that, indeed that's one of the advantages of Objective-C you can describe the parameters been received.

Take this for example:

- (BOOL)saveFile:(NSString *path, NSString *fileName);

On Objective C you paraphrase to self document the method like this:

- (BOOL)saveFileInPath:(NSString *)path withName:(NSString *)name;

2 Comments

Which is the actual variable name a method can use in that case, randomName or var1?
var1 is the variable name, randomName is part of the method declaration, is like if you paraphrase the method with their parameters instead of first declaring the method name and then listing all the parameters.
2

Objective-C syntax is aimed for clarity. So when you read a line of code, you know exactly whats happening.

For example:

[myData writeToFile:@"/tmp/log.txt" atomically:NO append:YES];

as opposed to

myData.writeToFile("/tmp/log.txt", false, true);

In my opinion, the first line of code is clearer. You know exactly what each arguments do just by reading the method call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.