gpt4 book ai didi

objective-c - NSScanner 通过引用返回的 NSString 的所有权

转载 作者:行者123 更新时间:2023-12-03 16:37:14 24 4
gpt4 key购买 nike

是否从方法中返回了“引用”NSString:

  • scanString:intoString:
  • scanCharactersFromSet:intoString:
  • scanUpToString:intoString:
  • scanUpToCharactersFromSet:intoString:

在调用实例拥有的 NSScanner 类中(在自动释放池中保留计数 1 且 NOT)或 NSScanner 实例(保留计数为 1 并在自动释放池中)?

如果是后者,如果用于迭代列表,自动释放池的大小不会膨胀吗?

最佳答案

value 参数

- (BOOL)scanString:(NSString *)string intoString:(NSString **)value;

Indirect parameters 意义上的“间接参数”在 Clang ARC 文档中:

Indirect parameters

If a function or method parameter has type T*, where T is an ownership-unqualified retainable object pointer type, then:

  • if T is const-qualified or Class, then it is implicitly qualified with __unsafe_unretained;
  • otherwise, it is implicitly qualified with __autoreleasing.

这里适用第二种情况。而 __autoreleasing 的意思是:

For __autoreleasing objects, the new pointee is retained, autoreleased, and stored into the lvalue using primitive semantics.

因此,value 指向从该函数返回时自动释放的对象。

Xcode 自动补全“知道”这一点并显示

[scanner scanString:(NSString *) intoString:(NSString *__autoreleasing *)]
<小时/>

添加:有关“手动引用计数”,请参阅 Memory Management Policy在《高级内存管理编程指南》中:

You Don’t Own Objects Returned by Reference

... In these cases, the same rules apply as have already been described. When you invoke any of these methods, you do not create the NSError object, so you do not own it. There is therefore no need to release it, ...

关于objective-c - NSScanner 通过引用返回的 NSString 的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16700171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com