Not understanding parentheses in Challenge


#1

Okay, I’ve gotten the Challenge code right (though I wish I had given my computer a more interesting name).

And I understand the + and -. + is used for a class method, and - for an instance method.

What I don’t get is the parentheses in lines like:

[code]+ (NSHost *)currentHost

  • (NSString *)localizedName[/code]

As far as I can tell, the parentheses (along with the + or - and the *) are a way to describe or indicate a method. Then we use that method to write lines like

NSHost *myMac = [myMac currentHost]; NSString *myName = [myName localizedName];

and so on.

What are the parentheses doing? And actually why don’t we do

[code]+ NSHost *myMac = [myMac currentHost];

  • NSString *myName = [myName localizedName];[/code]

or even

[code]+ (NSHost *)myMac = [myMac currentHost];

  • (NSString *)myName = [myName localizedName];[/code]

In another thread, somebody asked a similar question, and ended up realizing that

[code]+ (NSHost *)currentHost

  • (NSString *)localizedName[/code]

is how things are represented in the header file (which I know is the .h file).

So when the book says “this method returns a pointer to an NSHost object: + (NSHost *)currentHost” it means that that’s what we’d put in the .h file?

Is there a place in the book that discusses this and I just can’t find it? Sorry to be dense.


#2
+ (NSHost *)currentHost 
- (NSString *)localizedName

It is just the syntax of the Objective-C language for declaring method names.

You will get used to it in no time.

Just remember this rule for method declarations:
MethodDeclaration:= MethodType [color=#FF0000]b[/b][/color] MethodSignature

Thus + [color=#FF0000]([/color]NSHost *[color=#FF0000])[/color]currentHost declares a class-method, named currentHost, that returns an NSHost* and that doesn’t take any parameters.

MethodSignature contains the name of the method and also their names and types if the method takes any parameters as input. For example:
- (void)printFoo:(Foo*)foo atMost:(long)times

Here the method name is printFoo:atMost:, not just printFoo (note that the colons are included in the name).

If you are familiar with the C Programming Language (of which Objective-C is a super set), you will recognise the b[/b] construct as the type-cast or type-coercion operator. For example: b[/b], b[/b], b[/b], b[/b], b[/b], etc.

One day, you will become a fan of this weird but beautiful syntax.


#3

Hi -

Just pointing out that your examples are incorrect:

NSHost *myMac = [myMac currentHost]; NSString *myName = [myName localizedName];

i think you mean

NSHost *myMac = [NSHost currentHost]; NSString *myName = [NSString localizedName];


#4

[quote=“ibex10”][quote]
What I don’t get is the parentheses in lines like:
[/quote]

+ (NSHost *)currentHost 
- (NSString *)localizedName

It is just the syntax of the Objective-C language for declaring method names.

You will get used to it in no time.

Just remember this rule for method declarations:
MethodDeclaration:= MethodType [color=#FF0000]b[/b][/color] MethodSignature

Thus + [color=#FF0000]([/color]NSHost *[color=#FF0000])[/color]currentHost declares a class-method, named currentHost, that returns an NSHost* and that doesn’t take any parameters.

MethodSignature contains the name of the method and also their names and types if the method takes any parameters as input. For example:
- (void)printFoo:(Foo*)foo atMost:(long)times

Here the method name is printFoo:atMost:, not just printFoo (note that the colons are included in the name).

If you are familiar with the C Programming Language (of which Objective-C is a super set), you will recognise the b[/b] construct as the type-cast or type-coercion operator. For example: b[/b], b[/b], b[/b], b[/b], b[/b], etc.

One day, you will become a fan of this weird but beautiful syntax.[/quote]

The problem is that none of what you’ve stated was explained anywhere up to the first time we see it in the book. So it really doesn’t involve an issue of comprehension, it is just a surprise and not presented clearly.

Even one line stating a fraction of what you posted would have helped.


#5

[quote=“frotty”]Hi -

Just pointing out that your examples are incorrect:

NSHost *myMac = [myMac currentHost]; NSString *myName = [myName localizedName];

i think you mean

NSHost *myMac = [NSHost currentHost]; NSString *myName = [NSString localizedName];[/quote]

Both example will not work.

NSString doesn’t implement the method: localizedName. NSHost does. So, you would send the method to an instance of NSHost (myMac) in this case.

This is the correct way:

NSHost *myHost = [NSHost currentHost]; NSString *myMac = [myHost localizedName];

Edit to add:

In the first example - he is sending “currentHost” to an instance of the class. When intact currentHost is a class method - so your example for that is correct.