1

Possible Duplicate:
Method Syntax in Objective-C

I just started learning Objective-C and I'm a little confused about this statement.

+(NSMutableArray *) array;

This is what I understand:

  1. + means this is a method that responds to a class (which makes this a static method)

  2. NSMutableArray is an array that can have it's size change

  3. * pointer ( memory location reference)

  4. array is the name of the array that is of type NSMutableArray.

  5. () Why does this method need parentheses around the object pointer '(NSMutableArray *)'

I don't know how to conceptualize what these part mean as a whole. Can you explain this to me?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Leoa
  • 1,167
  • 2
  • 14
  • 31

2 Answers2

2

in C syntax this would be written as:

static NSMutableArray *array();

NSMutableArray * is the return type. array is the name of the method. There are no arguments, but if there were it would be like this:

+ (ReturnType)methodName:(ArgType)argName;

or if there were multiple arguments:

+ (ReturnType)firstPartOfMethodName:(Arg1Type)arg1Name secondPartOfMethodName:(Arg2Type)arg2Name;

This can be a point of confusion for a lot of Obj-C newcomers. The fact that the method name is split between the arguments can be extremely confusing to most programmers coming from other languages.

The reason that it's ordered that way is to give clarity to the arguments. When using methods like:

- (id)initWithBitmapDataPlanes:(unsigned char **)planes 
                pixelsWide:(NSInteger)width 
                pixelsHigh:(NSInteger)height 
             bitsPerSample:(NSInteger)bps 
           samplesPerPixel:(NSInteger)spp 
                  hasAlpha:(BOOL)alpha
                  isPlanar:(BOOL)isPlanar 
            colorSpaceName:(NSString *)colorSpaceName
              bitmapFormat:(NSBitmapFormat)bitmapFormat 
               bytesPerRow:(NSInteger)rowBytes
              bitsPerPixel:(NSInteger)pixelBits

(This is a real method from the Cocoa framework, known for being the longest), it's very helpful that you know which argument to place first, second, third, etc.

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
1

The 1,2,3 statements are correct. But fourth one is, array is the name of method. The fifth one is, the return type of array method is NSMutableArray *.

Finally array is the class method and it has NSMutableArray * return type and also does not any arguments.

It look like - (void)viewDidLoad. viewDidLoad is the instance method and it has void return type and also does not any arguments.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • There is also significance to why the name is `array`, in terms of memory management. In this case, `array` follows a Cocoa convention that is important to learn: any class method that begins with the class' basic type name (like `array`, `arrayWithObjects:`, and `arrayWithArray:`) is supposed to return an object that is automatically released. Unlike the expressions `[[NSMutableArray alloc] initWithCapacity:N]` and `[myArray copy]` (both of which must have `release` or `autorelease` called eventually), `[NSMutableArray array]` and similar methods have an implicit `autorelease`. – Kevin Grant Jul 07 '12 at 06:12