Parag Bafna
Parag Bafna

Reputation: 22930

Objective-c method without return type

Is it correct to write objective-c method like this

-methodA: (NSString *)str // without return type 
{
    return @"test";
}

Above method is working fine for me.
Is -methodA: (NSString *)str same as -(id)methodA: (NSString *)str?
I am using Mac 10.5 os x sdk.

Upvotes: 9

Views: 3695

Answers (7)

medvedNick
medvedNick

Reputation: 4510

I changed -init in my app:

-init
{
    ...
    return self;
}

and compiler did not show any warning, and if I change -init like that:

-(int)init
{
    ...
    return 0;
}

compiler shows warning about expected type, so I could say -init and -(id)init are the same methods, since I did not even know that compiler would pass this syntax and since checking language's features like that is bad practice :). I will read Apple's docs about Objective-C and try to find out something about that. Thanks for nice question :)

EDIT:

wow, you can read docs here, chapter Class Interface. (void), (NSObject*) and etc are only casts. Quote: "Method return types are declared using the standard C syntax for casting one type to another". And another quote: "If a return or parameter type isn’t explicitly declared, it’s assumed to be the default type for methods and messages—an id".

Upvotes: 3

hamstergene
hamstergene

Reputation: 24439

It is allowed, as 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.

Upvotes: 3

CRD
CRD

Reputation: 53010

To answer your question, from "The Objective-C 2.0 Programming Language":

If a return or argument type isn’t explicitly declared, it’s assumed to be the default type for methods and messages — an id.

So - methodA:(NSString *)str is valid and the same as - (id)methodA:(NSString *)str.

However having said that it is certainly unusual coding practice and not to be encouraged - type the extra four characters!

Upvotes: 13

Carl Veazey
Carl Veazey

Reputation: 18363

Even if it compiles it's not correct. It makes life slightly harder for other developers, including your future self.

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52575

Well if it compiles and it operates as you expect, then it works. But I wouldn't say that it's good practice.

If I'd had to guess, I would have assumed that your method would return nothing. So although "correct" it's misleading.

If you really do want to return an object of any type, would the extra four characters hurt? If it really returns nothing, set a return type of void. Pleasing the compiler is easy but not the whole story.

Upvotes: 1

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Do this:

- (void)methodA:(NSString *)str 
{
  //non returning method;
}

- (NSString *)methodA:(NSString *)str 
{
  return str;  //returning method;
}

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

- (void) methodA (NSString *)str //always start a methodod name lower case
{
  return;  //If you do not return anything then there is nothing to return. 
}

(id) is different from (void). You will have to return somthing here. id represents an object of any type. Only void represents 'nothing'.

Upvotes: 4

Related Questions