views:

163

answers:

4

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;
+4  A: 

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.

Chuck
That documentation is very good, although I didn't find whether I can interchange the attributes like:@property(copy, readwrite) NSString *value;@property(readwrite, copy) NSString *value;
okami
@okami: you can interchange the order, but there is an order that they are conventionally written in and deviating from that may be less useful for other people scanning your code.
corprew
+3  A: 

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.

corprew
+2  A: 

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.

Noah Witherspoon
+2  A: 

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.

Brian Campbell