NSString 赋值 &保留
关于 NSStrings 只是一个简单的问题,我有一段代码为字符串分配一个值,具体取决于发现的内容,它是通过 substringToIndex 或常量字符串 @"0.00" 分配的值,可以
// Save if value found, otherwise set to 0.00
if (parsedCharacters == nil || [parsedCharacters isEqualToString:@""])
self.currentDiscountedPrice = @"0.00";
else
{
// Truncate extra digits from string to 2 decimal places (find full stop, save 2 places after it)
NSRange fullStopRange = [parsedCharacters rangeOfString:@"."];
self.currentDiscountedPrice = [parsedCharacters substringToIndex:(fullStopRange.location + 3)];
}
用于分配,因为它将释放旧值 &保留新值?
无法知道在上一次迭代中 var 是否被分配了常量字符串或 substringToIndex 返回值,但我被告知调用retain &在常量字符串上释放是无害的,这是真的吗?
Just a simple question on NSStrings, I have a piece of code that assigns a value to a string, depending on what is found it is either assigned a value by substringToIndex or the constant string @"0.00", is it okay to use
// Save if value found, otherwise set to 0.00
if (parsedCharacters == nil || [parsedCharacters isEqualToString:@""])
self.currentDiscountedPrice = @"0.00";
else
{
// Truncate extra digits from string to 2 decimal places (find full stop, save 2 places after it)
NSRange fullStopRange = [parsedCharacters rangeOfString:@"."];
self.currentDiscountedPrice = [parsedCharacters substringToIndex:(fullStopRange.location + 3)];
}
for the assignment since it will release the old value & retain the new value?
There is no way of knowing whether the var was assigned the constant string or the substringToIndex returned value in the previous iteration but I was told calling retain & release on constant strings is harmless, is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSString 并不特殊;所有 Cocoa 对象都遵循 Cocoa 内存管理规则。只要你也这样做,你就会没事的。
是的。您应该像对待您不拥有的任何其他字符串一样对待它们:如果您想拥有它,则保留它,或者制作一个副本并拥有它;那么一定要释放你拥有的东西。
您应该在除
init
方法和dealloc
之外的任何地方使用该属性。这些是唯一应该显式向实例变量中的对象发送retain
和release
消息的方法。该段落两边的原因是您或子类可以为属性实现自定义访问器。在半初始化或半解除锁定的对象上运行自定义行为可能很危险,但您可能希望在其他地方都使用它。
将常量字符串对象传递给属性设置器没有什么坏处。它将照常保留或复制对象。
请注意,当属性的值是具有可变变体的类的对象(如 NSString 具有 NSMutableString)时,您应该将该属性声明为复制其值(
@property(copy)
或@property(nonatomic, copy)
),这样您就不会拥有其他人的可变对象的共同所有权。如果它们改变对象,这可能会给您带来问题,特别是如果您将对象存储在哈希集合(例如字典键)而不是实例变量中。NSString is not special; all Cocoa objects follow the Cocoa memory-management rules. As long as you do, too, you'll be fine.
Yes. You should treat them the same as any other string you don't own: Retain it if you want to own it, or make a copy and own that; then be sure to release what you own.
You should use the property everywhere but in
init
methods anddealloc
. Those are the only methods that should explicitly sendretain
andrelease
messages to the object in the instance variable.The reason for both sides of that paragraph is that you, or a subclass, may implement custom accessors for the property. The custom behavior may be dangerous to have run on a half-inited or half-deallocked object, but you'll probably want it everywhere else.
There is no harm in passing a constant-string object to a property setter. It will retain or copy the object as normal.
On that note, when a property's value is an object of a class with a mutable variant (as NSString has NSMutableString), you should declare the property as copying its value (
@property(copy)
or@property(nonatomic, copy)
), in order that you do not take co-ownership of someone else's mutable object. If they mutate the object, this could cause problems for you, particularly if you stored the object in a hashing collection (such as as a dictionary key) rather than in an instance variable.