- NSMutableString as retain/copy
- @property (nonatomic, retain) NSMutableString *str1;
- @property (nonatomic, retain) NSMutableString *str1;`
- // or
- @property (nonatomic, copy) NSMutableString *str1;`
- @property (nonatomic, retain) NSMutableString *str1;`
- @property (nonatomic, copy) NSMutableString *str1;`
- - (void)setStr1:(NSMutableString *)arg
- {
- /* locking/observing/undo/etc omitted */
- NSMutableString * copy = [arg mutableCopy];
- NSMutableString * prev = str1;
- str1 = copy;
- [prev release];
- }
- - (NSMutableString *)str1
- {
- /* locking/observing/etc omitted */
- /* don't return something the clients thinks they may
- possibly modify, and don't return something you may
- modify behind their back
- */
- return [[str1 mutableCopy] autorelease];
- // -- or???
- return [[str1 retain] autorelease];
- }
- @interface MONThing : NSObject
- {
- @private
- NSMutableString * str1;
- }
- /* note: passes/returns NSString */
- @property (nonatomic, copy) NSString * str1;
- @end
- @implementation MONThing
- // no need for clients to create a mutableCopy
- - (void)setStr1:(NSString *)arg
- {
- /* locking/observing/undo/etc omitted */
- NSMutableString * copy = [arg mutableCopy];
- NSMutableString * prev = str1;
- str1 = copy;
- [prev release];
- }
- // the result is clearly defined. return a copy for
- // thread-safety, expected behavior, and to minimize
- // further copies when handling the result.
- - (NSString *)str1
- {
- /* locking/observing/etc omitted */
- /* don't return something the clients thinks they
- may possibly modify, and don't return something
- you may modify behind their back
- */
- return [[str1 copy] autorelease];
- }
- @end
- @interface MONThing : NSObject
- {
- NSString * str1;
- }
- @property (nonatomic, copy) NSString * str1;
- @end
- @implementation MONThing
- @synthesize str1;
- - (void)updateTimeElapsed:(NSTimeInterval)seconds
- {
- NSMutableString * s = [NSMutableString stringWithFormat:@"%f seconds", seconds];
- /* ...some mutations... */
- self.str1 = s;
- }
- @end