Getter and setter explained?

I'm just learning OOP from a book I took (Big Nerd Ranch), and it just went through the head of the getter and setter. I just wanted to clarify, I understand what I just did. Instead of creating a method to set the value of the instance, and then another method to extract the value and display it, I create using the syntax @property and @synthesize to define both methods.

Instead of this:

 -(void) setHeightOfObject:(int)h; -(void) setWeightOfObject:(float)w; -(int) heightOfObject; -(float) weightOfObject; 

and defining it as follows:

 - (int)heightOfObject { return heightOfObject; } - (void)setHeightOfObject:(int)h { heightInMeters = h; } - (float)weightOfObject { return weightOfObject; } - (void)setWeightOfObject:(float)w { weightOfObject = w; } 

I would do this with getter and seters in a .h file:

 @property int heightOfObject; @property float weightOfObject; 

And then go to my .m file and link it:

 @synthesize heightInMeters, weightOfObject; 

Does this give me the opportunity to set the value of my object and then get it if I need to print it? I know this is an important concept, and I want to make sure that I understand it correctly.

+4
source share
4 answers

You're right. @synthesize essential extends the implementation that you wrote at compile time.

Since writing getters and setters is boring and repeating (and most objects have a bunch of properties for which you need getters and setters), with this small shortcut, you spend less time on boilerplate code and more time on implementing something interesting.

If you want more detailed information about the properties of objective-c, you can read the programming guide (although this may be somewhat unreasonably detailed for you at the moment).

+3
source

There are two parts to what you achieve using @property and @synthesize.

@property tells the compiler that it should allow you to use dot syntax to call the heightOfObject and weightOfObject accessories. So this is

 int height = myObject.heightOfObject; myObject.weightOfObject = 10; 

becomes a legal code and is exactly equivalent to this:

 int height = [myObject heightOfObject]; [myObject setWeightOfObject:10]; 

You can use @property without @synthesize, in which case you should implement accessors exactly as you did in your question.

Using @synthesize tells the compiler that it should generate accessors for you, and it will also generate instance variables themselves if your runtime supports it (for example, on iOS and 64-bit OS X).

+1
source

Property and synthesis were introduced in Objective-C 2.0 to provide an easy way to create getters and setters.

Check this link:

http://cocoacast.com/?q=node/103

0
source

You not only get getters and setters. You also get pure syntax: self.heightOfObject, which you can assign or read. @property has many settings, so you can read them in more detail. In particular, you can control whether you need both read and write access, or just one of them.

0
source

Source: https://habr.com/ru/post/1390494/


All Articles