Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sheepopo39

macrumors 6502
Original poster
Sep 18, 2008
251
0
I am currently reading a book on objective-c , and I am on the concept of "Setters" and "Getters". The book states that a setter is something that sets the value of instance variable (which I understand), it also says that a getter is something that retrieves the value of on an instance variable. Where I get stuck is that the example which was working with fractions, was that the methods that outputted the numerator and denominator were the getters, which I don't understand because, you're not retrieving any values, you're outputting it. I am pretty confused, I'm hoping that somebody could try and help explain it to me.

Thanks,

John.
 
I am not so sure "getter" should be used in that way. What it seems like they are trying to convey is that your object supplies its content to the sender through methods, which can simply deliver the value or can deliver an interpreted value. Mainly, they want you to perceive your object as opaque to other objects and rely on methods for moving content amongst them.
 
That does seem odd, but one convention Apple uses is to call methods that return a value into a pointer you pass getXXX. For example:

Code:
char *start, *end;
[NSSomething getStartCharacter:start andEndCharacter:end];

But yeah, normally an Objective-C "getter" method simply returns an instance variable value and by convention the method has exactly the same name as the variable itself.
 
The thing about Object Oriented Programming is that it's designed to be modular. You can think of it a bit like modern stereo equipment where you have these universal interfaces to each other (those audio-video jacks for inputs and outputs) but then the internals of each device is secret to all of the other components.

What's cool about a setup like this is that if the time comes when you want to swap out your dvd player for a blue-ray player, you just need to switch the plugs and you don't have to touch any other parts of the system and it will just work. In other words, your tv doesn't need to know if there's a vhs tape deck, a dvd player or an Apple TV on the other end of the cord as long as when it asks: "give me your picture" the device will send it the signal it expects (I realize stereo components don't actually send and receive messages like this, but it's a metaphor).

So you can think of your setters and getters a bit like the in and out ports on a stereo. You're not supposed to ever directly access the goodies inside the "secret box" but you use the "public interfaces" which are like those ports that connect universally to the outside world--that way if things change on the inside of the box all of the other components will still work fine without having to make changes to them. I hope that helps a bit.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.