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{
}
selytch
  • 535
  • 2
  • 9
  • 24
  • 1
    It's based on [Smalltalk's message syntax](http://en.wikipedia.org/wiki/Smalltalk#Messages); 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? – jscs Feb 27 '13 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? – selytch Feb 27 '13 at 20:58

3 Answers3

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
puzzle
  • 6,071
  • 1
  • 25
  • 30
  • 1
    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). – bbum Feb 28 '13 at 00:14
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;
Community
  • 1
  • 1
Paul N
  • 1,901
  • 1
  • 22
  • 32
  • Which is the actual variable name a method can use in that case, randomName or var1? – selytch Feb 27 '13 at 20:54
  • 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. – Paul N Feb 27 '13 at 20:57
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.

Jean-Francois Gagnon
  • 3,141
  • 4
  • 20
  • 27