small medium large xlarge

Generic-user-small
04 Apr 2009, 02:56
Vip Malixi (7 posts)

Hi.

Great Screencast. I have the original NextStep Object C book and couldn’t make heads or tails with it until I viewed your screencast so great job–you’re a wonderful teacher.

Now on to my question: in Movie.m, you set title (setTitle:) using the simple code:

title = newTitle;

or more correctly…

title = [newTitle capitalizedString];

but with the customized initialization, i.e., [initWithTitle: andRating:] you use “self.title” as in:

self.title = newTitle; self.rating = newRating;

why not just use…

title = newTitle; rating = newRating;

I tried this and it worked. Is there a specific reason we should use “self.title”?

Thanks.

Biopic_100x100_pragsmall
04 Apr 2009, 06:14
Bill Dudney (917 posts)

Hi Vip,

Thanks for the kind words.

title = newTitle;

is not invoking the set method. i.e. title=newTitle; is just an assignment where as

self.title = newTitle;

is the same as calling the set method;

[self setTitle:newTitle];

calling the set method does the memory management stuff you specified int he attributes of the property so they are very different.

Good luck!

Generic-user-small
04 Apr 2009, 23:45
Vip Malixi (7 posts)

Hi. Thanks for the reply. So it has to do with memory management stuff. I’m just wondering why this would work–will memory be affected negatively because I didn’t use the ‘self.title’ syntax?:

- (id)initWithTitle:(NSString *)newTitle
		  andRating:(int)newRating {
	self = [super init];
	if(nil != self) {
		title = newTitle;
		rating = newRating;
	}
	return self;
}

versus the original code (which also works):

- (id)initWithTitle:(NSString *)newTitle
		  andRating:(int)newRating {
	self = [super init];
	if(nil != self) {
		self.title = newTitle;
		self.rating = newRating;
	}
	return self;
}

Also, I wanted to mention that Stanford is offering their iPhone SDK course on the iTunes U podcast–and I wouldn’t have understood the code sample they wrote so quickly if not for your screencast, Bill, so again, you have this very rare ability to clarify and pick the most important points to explain. Anyone thinking of downloading the Stanford podcast should consider watching your screencasts first so they have a better understanding of the Stanford course.

Biopic_100x100_pragsmall
05 Apr 2009, 05:10
Bill Dudney (917 posts)

Hi Vip,

In your code newTitle is not retained, so if it were to be released by the calling context you’d have a pointer to freed memory. So your code has an error in it. Remember if you want to use an object beyond the scope of the method its created in you must retain it.

Good luck and thanks again for the kind words.

You must be logged in to comment