Segev
Segev

Reputation: 19303

can't understand setters and getters in objective c

I'm learning objective c with some online tutorials and i have a question. On this video: http://www.youtube.com/watch?v=Nb9-1rRjO_8&feature=player_detailpage#t=367s you can see the code i wrote here:

@synthesize width, height; //setters and getters

-(void) setWH:(int) w:(int) h{
    width = w;
    height = h
}

I don't understand why he wrote a setHW method if he already got setters and getters for width and height with @synthesize. What am i missing?

Thanks

Upvotes: 0

Views: 143

Answers (2)

Abizern
Abizern

Reputation: 150755

He is synthesising width and height, but the method you are asking about setWH looks lie a badly named convenience method to set the width and height in one place.

Actually, it looks quite bad, apart from not using self.width and self.height as he should do, having a set for something that isn't a property looks odd.

Where's the init?

Find a better teacher.

If you like watching videos - fire up iTunes, go to iTunesU look up C193P, which is a video set of Stanford's iPhone programming course. You'll learn a lot more correctly there.

Edited to add

The link to this course on iTunes is https://itunes.apple.com/gb/itunes-u/ipad-iphone-application-development/id473757255

Upvotes: 5

mttrb
mttrb

Reputation: 8345

I've had a look at the linked video and I don't think it is a very good resource for learning Objective-C.

The "setWH" method as you surmise is unnecessary and has a very confusing declaration.

-(void) setWH:(int) w:(int) h

Are w and h parameter variables or is w: part of the method name. If w: is part of the method name then why is the parameter variable called h. If w is a parameter variable then the method name is actually setWH::.

In fact both w and h are parameter variables which means you call this method as follows:

[obj setWH:10 :10]; 

This is not idiomatic Objective-C.

Upvotes: 3

Related Questions