small medium large xlarge

Generic-user-small
29 Dec 2009, 13:34
Gary Tamblyn (3 posts)

I think the Objective-C tutorials are pretty good, but have a few questions.

From 25 minutes into the first tutorial an NSString variable is given the name ‘title’ then just after this, a method is declared called ‘title’.

Is it ok / usual in objective-C to give methods and variables the same name, I seem to remember variables and functions shouldn’t be given the same name when programming in C.

Also I’m finding the use of * a bit confusing, or at least the syntax positioning of it.

When the title string is declared NSString *title; There seems to be a deliberate space after NSString and the asterisk. so my assumption would be that the * and ‘title’ are together as one thing.

This deliberate space looks a little strange in the method declaration when the brackets suggest otherwise.

-(NSString *)title;

Is the * part of the return type NSString* or part of the method name *title ? or a different answer :-)

Forgive my ignorance, I’m just starting out in Objective C and get confused very easily.

Biopic_100x100_pragsmall
29 Dec 2009, 15:18
Bill Dudney (917 posts)

Hi Gary,

All objects in objective-c must be pointers to objects, thus the *.

So the return type of -(NSString *)title; is a pointer to an NSString object, but the ‘pointer to’ is assumed and rarely stated.

Sorry if I did not make that clear in the initial part of the screen cast.

No problem about the question, that is what I’m hear for.

Good luck!

Generic-user-small
29 Dec 2009, 16:25
Gary Tamblyn (3 posts)

Many thanks for your quick reply Bill, It’s very much appreciated. Hope you had a good Christmas.

I think the use of ‘title’ twice perhaps confused me.

If I understand it right, the initial line is a string declaration of type NSString and the name of that string is *title (i.e. it is a pointer).

The method declaration -(NSString *)title is a method that returns an NSString Pointer and the name of this method is coincidentally ‘title’, but the two occurrences are unrelated, one is an instance variable that stores a string, the other is a method that performs a function defined in the @implementation area of the .m file.

I’m guessing the return type could equally have been written as -(NSString*) title with no space between the NSString and the * ?

I’m just trying to understand if the *, syntactically, belongs to the NSString (i.e. NSString* title) in the string declaration or if it belongs with the name of that string (i.e. NSString *title).

Biopic_100x100_pragsmall
29 Dec 2009, 17:42
Bill Dudney (917 posts)

Hi Gary,

You are getting it :)

The instance variable ‘title’ and the method ‘title’ are in different scopes so they don’t conflict. However it is very typical to see methods named after instance variables. In fact ‘title’ the method is the accessor for ‘title’ the instance variable, there typically is a ‘setTitle:’ method that takes an argument of type ‘NSString *’.

Yes it could have been written ‘NSString*’ instead of ‘NSString *’. Arguments abound on both sides of ‘space after class name’ convention. I’m not passionate about it enough to even say ‘meh’. So go with whatever feels more natural to you.

A good book on programming in C might help you with the pointer stuff Kelly and Pohl’s A Book on C is where I learned but its been a while since it was updated.

Once you can accept that objects are pointers you don’t need much C but there will be things that are confusing (structs, typedef, etc.) if you are’t willing to just accept them. So that book on C programming might be a good buy.

Good luck!

Generic-user-small
29 Dec 2009, 19:04
Gary Tamblyn (3 posts)

Brilliant, thanks that’s cleared things up.

Probably if I ever get competent at programming in Objective-C I’ll look back and the asterisk positioning would look like a silly question, but I guess during the learning process any small thing can throw me off course.

I bought a VTC training course about C a few years ago by Mark Virtue

http://www.vtc.com/modules/products/titleDetails.php?sku=33256&affiliate=trainingclasses&tcsrc=www.training-classes.com

Without doubt it’s the best video training course I’ve ever watched on any computer topic and would recommend it to anyone starting out in C, or anyone doing a training course, although it’s also probably a bit out of date. I will re-watch the section on Pointers. :-)

Many thanks for your help and Happy new year :-)

You must be logged in to comment