保留和保留的区别
这些保留之间有什么区别?:
@property (nonatomic, retain) NSString *A_StringToRetain;
因为
NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];
使用属性,字符串不会保留并使用第二种方式,一切都可以,但我需要检查和释放以避免泄漏。
示例:.m
中的 .h
中声明,
NSString *A_StringToRetain;
@property (nonatomic, retain) NSString *A_StringToRetain;
我在我使用的
A_StringToRetain = @"MyValue";
但是当我退出方法时,我丢失了 A_StringToRetain
。这是一个僵尸。
如果我不使用属性并以这种方式声明字符串,则
NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];
该字符串位于内存中。
谁能告诉我为什么?
分配/保留的方式不一样吗?
有什么区别?
谢谢。
what's the difference between these retains?:
@property (nonatomic, retain) NSString *A_StringToRetain;
and
NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];
Because using property, string won't retain and using second ways, all ok, but i need to check and release to avoid leaks.
Example:
I declared in .h
NSString *A_StringToRetain;
@property (nonatomic, retain) NSString *A_StringToRetain;
in .m
i use
A_StringToRetain = @"MyValue";
but when i exit from method, I lost A_StringToRetain
. It's a zombie.
If i not use a property and declare a string in this way
NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];
the string is in memory.
Anyone tell me why please?
It's not the same way to alloc/retain?
What's differences?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您直接设置变量而不是通过属性(应用保留)...您需要...
或者
这里的关键是
self.
这意味着您通过属性并且不直接到 ivar 本身。You are setting the variable directly and NOT going through the property (which applies a retain)... you either need...
or
The key here is the
self.
which means you go via the property and not directly to the ivar itself.