NSString 赋值 &保留

发布于 2024-09-13 15:36:37 字数 694 浏览 10 评论 0原文

关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-09-20 15:36:41

NSString 并不特殊;所有 Cocoa 对象都遵循 Cocoa 内存管理规则。只要你也这样做,你就会没事的。

有人告诉我调用retain &在常量字符串上释放是无害的,这是真的吗?

是的。您应该像对待您不拥有的任何其他字符串一样对待它们:如果您想拥有它,则保留它,或者制作一个副本并拥有它;那么一定要释放你拥有的东西。

...我应该明确地释放旧值吗?保留 substringToIndex 值,或者只使用 self.currentDiscountedPrice = 进行这两个分配?

您应该在除 init 方法和 dealloc 之外的任何地方使用该属性。这些是唯一应该显式向实例变量中的对象发送 retainrelease 消息的方法。

该段落两边的原因是您或子类可以为属性实现自定义访问器。在半初始化或半解除锁定的对象上运行自定义行为可能很危险,但您可能希望在其他地方都使用它。

将常量字符串对象传递给属性设置器没有什么坏处。它将照常保留或复制对象。

请注意,当属性的值是具有可变变体的类的对象(如 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.

I was told calling retain & release on constant strings is harmless, is this true?

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.

… should I rather explicitly release the old value & retain the substringToIndex value or it okay to just use self.currentDiscountedPrice = for both assignments?

You should use the property everywhere but in init methods and dealloc. Those are the only methods that should explicitly send retain and release 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文