开发者

@property question

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 rel开发者_开发技巧ease];
    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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜