0

合成访问器方法有一个复制属性,例如:

@property (copy) NSMutableString *string;

使用此 setter 方法进行分配时,它似乎总是调用 copy 方法,即使我想string在分配实例变量期间创建我分配给的内容的可变副本string

这是一个已知问题,是否有任何解决方法?

谢谢 :-)

4

1 回答 1

1

Don't call @synthesize string in your .m implementation file and instead write your own getter.

e.g.

- (NSMutableString *) string
{
    NSMutableString * stringToReturn = [NSMutableString stringWithString: someStringObject];
}

More information about properties (and what to do when you don't do @synthesize) can be found at:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

p.s. the property name "string" may be confusing to anyone else who looks at your code down the road... I'd recommend changing that to be more program-specific

于 2011-11-07T02:13:28.283 回答