1

In my implementation I have getters and setters like below. I want to use properties and synthesize the getters and setters but have a few questions.

- (NSString *)title {
return title;
}

- (void)setTitle:(NSString *)value {
if(title != value) {
    [title release];
    title = [value retain];
}
}

If I was to convert that to a property, what attributes would I use? Am I right in thinking:

  • readwrite so both getters and setters are present
  • retain so that it increase the retain value of the value string so the object don't lose it.

Am I right with the above?

One final thing. I have the method below ...

- (void)setReleaseDate:(NSString *)value {
// YYYY-MM-DD HH:MM:SS +HHMM
if([releaseDate description] != value) {
    [releaseDate release];
    releaseDate = [[NSDate alloc] initWithString:value];
}
}

Am I right in thinking I still have to include that method because it contains code that the synthesized getter would not include?

Thanks.

3 Answers3

1

Your assertion about using readwrite as well as retain is correct as it would create semantically equivalent code to what you have posted.

The releasedate property setter can't be synthesized as you're transforming a NSString into a NSDate to store it, that also avoids common issues with NSString properties, for which you'd better use copy to avoid problems with NSMutableString.

Other than that, your code is fine, except that for string comparison you may want to replace the simple pointer check != with isEqualToString, see Comparing Strings in Cocoa.

Community
  • 1
  • 1
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • Thank you. Quick question, the with the releaseDate property would I create it read only and manually add the setter? Or would it be best not to add a property for it at all? – James Guvna Redmond Mar 06 '11 at 11:49
  • as James Bedford already explained, I would provide acces to the releaseDate via a synthesized NSDate property and have the string method as separate convenience method. – Johannes Rudolph Mar 06 '11 at 13:56
1

For your title property, you can declare it in your class interface as follows:

@property (nonatomic, retain) NSString* title;

Which is the same as the following:

@property (readwrite, nonatomic) NSString* title;

readwrite is a default setting. Most of the time you will want setters for your properties, so for the times when you don't you would use the non-default readonly to specify this.

The nonatomic part basically means that the accessors will be faster, and is typically used. You can find out more information about this here: What does the property "Nonatomic" mean?.

For your second question, you can implement your own accessors if you wish. If you do this then it kind of 'overrides' the accessor that would be generated by Objective-C. Remember that you have to keep to the naming conventions. So in your example, the "setReleaseDate:" method you've defined would be used for the setter method for the property "releaseDate" - which is completely correct! :) The problem you have though is that you're passing an *NSString** to set the date, which means that this method won't override the default setter that would be used if you synthesized the property. You have to pass a value of the same type as the one you're setting as the single argument, so for this case you would have to pass an *NSDate**.

You must also ensure that if you provide your own implementation of an accessor that it does what is declared in the interface. I presume your releaseDate property should be declared as retain.

Community
  • 1
  • 1
James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • If I provide my own implementation of the releaseDate accessor, in my case I know I will be overriding the default, should I create the property as read so that it don't create the accessor? Although, I think for the sake of correctness passing in an NSDate would seem like the right thing to do. Any advice? – James Guvna Redmond Mar 06 '11 at 12:18
  • Passing in an `NSDate` would be the right thing to do, but there's nothing wrong with also having a convenience method that does the string translation. You need to distinguish between the property declaration and the synthesized methods: the property is still `readwrite`, because you can write to it. You want to be able to use dot syntax to assign: `self.releaseDate = x` won't work for `readonly`. `@synthesize` will only create an implementation when you haven't already, so it's safe (eg) to write your own setter and use `@synthesize` to create the getter. – walkytalky Mar 06 '11 at 13:04
  • `@property (nonatomic, retain) NSString* title;` is not the same as `@property (readwrite, nonatomic) NSString* title;`. The compiler will show a warning too. If there is no retain, assign or copy then he will assume that you meant assign. And assign is definitely not the same as retain. – Matthias Bauch Mar 06 '11 at 13:46
0

It is common, though not always required, to use copy semantics for NSString properties, to avoid issues with NSMutableString objects being changed behind your back.

Otherwise, you seem to be pretty much on top of it.

walkytalky
  • 9,453
  • 2
  • 36
  • 44