如何解决__strong 限定符与函数参数的作用是什么,尤其是在 case 函数中 (const __strong NSString *const paths[])
在 github 上的代码中发现了类似的行,我无法理解为什么需要 __strong,尤其是在 C 数组的上下文中。
编写了一个测试代码,无论是否使用 __strong 关键字,它似乎都可以运行。
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
static unsigned long getRetainCount (id obj)
{
SEL s = NSSelectorFromString (@"retainCount");
return (((NSUInteger (*)(id,SEL))objc_msgSend) (obj,s));
}
static NSArray *function (const __strong NSString *const paths[],int count)
{
NSMutableArray *anArray = [NSMutableArray array];
if (count >= 2) {
NSLog (@"(fA) Retain for [1]: %lu",getRetainCount(paths[1]));
[anArray addObject:paths[0]];
[anArray addObject:paths[1]];
NSLog (@"(fB) Retain for [1]: %lu",getRetainCount(paths[1]));
}
return (anArray);
}
int main (int argc,const char * argv[])
{
@autoreleasepool {
NSString *otherPaths[2];
NSArray *myArray = nil;
otherPaths[0] = @"Hello";
otherPaths[1] = [NSString stringWithFormat:@"%@ - %s",@"World","Again"];
NSLog (@"(mA) Retain for [1]: %lu",getRetainCount(otherPaths[1]));
myArray = function (otherPaths,2);
NSLog (@"(mB) Retain for [1]: %lu",getRetainCount(otherPaths[1]));
}
return 0;
}
输出为:
(mA) Retain for [1]: 3
(fA) Retain for [1]: 3
(fB) Retain for [1]: 4
(mB) Retain for [1]: 4
如果我将 otherPaths[0] 放在 NSLog() 中,它会保留 MAX_UINT 的计数,因为它是一个静态字符串。
__strong 是否有任何方法或函数的参数使用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。