开发者

@property objective-c syntax

开发者 https://www.devze.com 2023-01-01 08:44 出处:网络
I\'m looking for the syntax of the getter/setter. Which is the setter and which is the getter?? 开发者_开发百科

I'm looking for the syntax of the getter/setter. Which is the setter and which is the getter??

开发者_开发百科

Is the readwrite attribute the getter?

Is the assign the setter?

@interface SomeClass : NSObject
{
  NSString *str;
  NSDate *date;
} 

@property (readwrite, assign) NSString *str;
@property (readwrite, assign) NSDate *date;


Neither is the getter or the setter. readwrite controls whether a set method is generated or just a getter, and assign specifies the memory management scheme (in this case, the variables are not retained, which is probably a mistake).

For the full property declaration syntax, you can take a look at the relevant docs.


You should check out this page, it will explain things.

readwrite = the property can be read and written

assign = this is a property that doesn't need to be ref counted. (the alternative is 'retain,' which means that values of this property are retained when set and released when overwritten.


Neither. The code you posted is an interface declaration; getters and setters go in an @implementation context, and are usually created using the @synthesize directive, as in

 @synthesize str;
 @synthesize date;

There are a number of attributes that can go after a property declaration. In this case, the readwrite specifies that the value of the property can be set (using the someObject.str = @"foo" syntax); the opposite is readonly, which means that the value of the property cannot be set. assign—as opposed to copy or retain—means that the property's value gets set directly, whereas the latter two create a copy of the value and retain the value, respectively.


The getter and setter are two methods that are automatically created when you use @property. By default, the getter will have the same name as the property, the the setter will have the name prefixed with set and suffixed with :; for instance, for the property str, you would be able to call [someobj str] to get the str property, and [someobj setStr: somestr].

The readwrite and assign attributes provide some information about how this getter and setter should be defined, if you use @synthesize to create the definitions for you. readwrite simply says that you are allowed to set the property, and assign says how the property will be set. See the documentation for more info.

0

精彩评论

暂无评论...
验证码 换一张
取 消