Objective-C getter/ setter

本秂侑毒 提交于 2019-11-29 04:01:26

What i dont understand is this line in main: myRect.origin = myPoint; I did not make a setter for it..

There is both a getter and a setter (collectively referred to as accessors) created for origin in the Rectangle class. If you have a look in the implementation for Rectangle, this is the getter:

-(XYPoint *) origin
{
    return origin;
}

and this is the setter:

- (void) setOrigin: (XYPoint *) pt
{
    origin = pt;
}

And as of Objective-C 2.0 calling:

myRect.origin = myPoint;

is equivalent to:

[myRect setOrigin:myPoint];

Declaring getters and setters using @property (and then implementing them using @synthesize) is only one way of declaring and creating accessors, and is there for a convenience if you have lots of properties to declare in the class interface. As Schildmeijer said, @property int width is equivalent to declaring two methods:

- (int)width;
- (void)setWidth:(int)newWidth;

Due to the dynamically-bound nature of Objective-C method calls, you don't even have to declare the getter and setter methods in the interface, although it is generally best practice to do so if you are advertising them as publicly available to other classes.

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

@property int width;

is equivalent to:

- (int)width;

- (void)setWidth:(int)newWidth;
//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property int Width;
@property int Height;
-(int)Area;
@end

//Rectangle.m
#import "Rectangle.h"
@implementation Rectangle
@synthesize Width;/*Will create value Width , Setter called"setWidth" and Getter called "Width"*/
@synthesize Height;/*Will create value Height , Setter called"setHeight" and Getter called "Height"*/

-(int)Area
{
    return Width*Height;
}
@end


//  main.m
#import <Cocoa/Cocoa.h>
#import "Rectangle.h"
int main(int argc, const char * argv[])
{
    Rectangle *myRectangle = [Rectangle new];

    myRectangle.Width=3;
    myRectangle.Height=5;
    printf("Area = %d\n",[myRectangle Area]);

    //Or
    [myRectangle setWidth:5];
    [myRectangle setHeight:6];
    printf("Area = %d\n",[myRectangle Area]);

}

If you want to make Getter only or rename getter and setter

• readonly

• getter = newGetterName

• setter = new SetterName

example

//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (getter = getWidth) int Width;
@property (readonly) int Height;
@end

You don't say what code is working, or what your expectations are for "working".

The above interface will create simple accessor methods for width and height that can be called from other objects as [object setWidth:1]; or object.width = 1; - these two are analogous.

Origin is some other object type and is a pointer, yes. But you would still want to declare a property for it to generate accessor methods.

Getters and setters are mostly useful if you need to access an instance variable from another class or you're using bindings to get/set them. So my guess would be that you need this functionality for the width and height but not for the origin. Note that the getters/setters do not make pointers out of the integers as you stated might be the reason. Ints are ints and getters/setters do not change that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!