The quickest and safest thing to do would be to add @synthesize value
to the top of your implementation, and the compiler will automatically generate these methods.
The issue of copy vs. retain hinges on the fact that you may be passed in a NSMutableString, which would change its value. If you have a setter for an 'immutable' type (string, set, array, dictionary), you need to use copy
semantics. At first this may seem counterintuitive (why make a copy if it's immutable?) but the thing to realize is that your class wants to assume it is immutable, and what's passed in may not actually be immutable.
NSMutable classes implement the copy
selector by returning an immutable version of what they represent. The immutable classes (NSString, etc) implement copy
with a retain
call. That is to say, they are very fast.
Your setter also needs to release value
before assigning it a new value. The proper code is:
-(void)setValue:(NSString*)newvalue
{
if (value != newvalue)
{
[value release];
value = [newvalue copy];
}
}
If you have complete control over all the classes that may call setValue:
and are absolutely sure you won't be passing in NSMutableString, you can use retain
, but it's best practices to use copy
.