-1

Consider the below prototype, which is a common one,

-(void)writeData:(NSData *)data length:(NSInteger *)len;

I am aware what each part of this method means. My query is, whether the "length", the name for the second argument is optional? Whether the compiler will compile the following:

-(void)writeData:(NSData *)data :(NSInteger *)len;

*Edit:*Thanks for all your responses :)

jxgn
  • 741
  • 2
  • 15
  • 36

5 Answers5

2

The compiler will not complain if you leave out the name tag for the second parameter (try it out to see for yourself to see that it works).

However, it is not optional in calls of methods where it is defined. For example, you cannot call the first method with the length portion of the method name omitted.

Also note that the colon is not optional when making a selector for the second method:

SEL(writeData::)
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

It will, but it is considered bad style - basically you are violating the Objective-C naming conventions.

The first method is named writeData:length:, the second one writeData::

eik
  • 2,104
  • 12
  • 15
2

Firstly, (NSInteger *) is not fine, because NSInteger is primitive type. So you must use only NSInteger.

Also, your second syntax is true for compiler, but wrong for human.

Arun
  • 3,406
  • 4
  • 30
  • 55
lykant
  • 516
  • 4
  • 17
  • 2
    Yes it valid but not fine ;), NSInteger is a typedef for int, you don't have to declare it as a pointer. – Tarek Hallak Sep 12 '13 at 13:35
  • "NSInteger *" is a pointer to such an integer. It's confusing. There is a good answer: http://stackoverflow.com/questions/1014053/why-dont-i-declare-nsinteger-with-a – lykant Sep 12 '13 at 13:39
1
-(void)writeData:(NSData *)data :(NSInteger *)len; 

Is a valid objective-c method (and it is different from writeData:length: from compiler point of view), but naming all method parameters makes code much more readable and easier to understand, so your 1st example is preferable

Vladimir
  • 170,431
  • 36
  • 387
  • 313
1

The second one is valid but unused and less descriptive. In the first case, the method name is writeData:length:, I mean, "lenght:" is part of the method name. The second method name would be writeData::. I have never seen using "anonymous" parameters in ObjC... in fact is one of the things I like most from the language.

e1985
  • 6,239
  • 1
  • 24
  • 39