【发布时间】:2013-05-20 03:10:52
【问题描述】:
我声明一个强属性:
@property (strong) NSString *message;
我将消息设置为:
self.message = [NSString stringWithFormat:@"xxxx %@",sth];
但它会因消息而崩溃:
*** -[CFString retain]:消息发送到已释放实例 0x1015ea790即使我更改了属性:strong-> copy,它仍然会崩溃。 现在我修复了它:
self.message = [[NSString stringWithFormat:@"xxxx %@",sth] copy];
但我还是看不懂,因为我在 iOS 里总是这样写。
顺便说一句:代码是可可的,无弧
更新1:
1 我已添加@synthesize message;
2 sth是一个例子,真正的代码是
self.message = [NSString stringWithFormat:@"xxxx %@", [[NSDate date] description]];
3 我记得 strong/copy 属性的默认实现可以是:
- (void)setMessage:(NSString*)newMsg
{
if (message != newMsg)
{
[newMsg retain];
[message release];
message = newMsg;
}
}
所以我认为属性综合会为我添加副本/保留。 这就是让我感到困惑的原因!
【问题讨论】:
标签: objective-c cocoa properties memory-leaks