Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C method syntax

I was surprised when the following method definition compiled (using Apple LLVM 4.1):

- (void) testMethod:someArgument {

}

Notice the type of someArgument is missing. What's the rule in Objective-C about specifying the types of method arguments?

like image 473
SundayMonday Avatar asked Oct 01 '12 14:10

SundayMonday


2 Answers

The default argument type is id. Even this will compile:

- testMethod:someArgument {
}

This is a method that takes an id as its argument and should return an id.

Actually, not even the method name is necessary:

- :someArgument {
}

This can be called as:

[self :someObject];

Of course all of this is very bad practice and you should always specify the types (and the names).

like image 89
DrummerB Avatar answered Sep 20 '22 23:09

DrummerB


Language specification 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.

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/chapters/ocDefiningClasses.html

like image 27
Tomasz Wojtkowiak Avatar answered Sep 20 '22 23:09

Tomasz Wojtkowiak