gpt4 book ai didi

ios - __strong 非 ARC 项目中使用的限定符

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:54 25 4
gpt4 key购买 nike

该项目未启用 ARC,但我们(错误地)使用符合 ARC 的代码库 - 特别是一个用于创建 GCDSingleton.h 中定义的单例对象的代码库:

#define DEFINE_SHARED_INSTANCE
+ (id)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = ^{return [[self alloc] init];}();
});
return _sharedObject;
}

即使共享对象是用 __strong 限定符定义的,这似乎也有效。我想知道为什么这不会导致错误或至少不会导致警告(最新的 Xcode 4.6 和 ios 6 sdk)。此外,由于该项目未启用 ARC,那么 __strong 限定符到底在做什么(如果有的话)?

最佳答案

在 MRC 代码中,__strong 被简单地忽略了。

我试着编译了一个简单的例子

#import <Foundation/Foundation.h>

int main(int argc, char const *argv[]) {
__strong NSString * foo = [[NSString alloc] initWithFormat:@"Hello, %s", argv[1]];
NSLog(@"%@", foo);
}

用圆弧

clang -fobjc-arc test.m -S -emit-llvm -o arc.ir

没有ARC

clang -fno-objc-arc test.m -S -emit-llvm -o mrc.ir

并区分 llvm IR 输出。

这是diff mrc.ir arc.ir的结果

54a55,56
> %17 = bitcast %0** %foo to i8**
> call void @objc_storeStrong(i8** %17, i8* null) nounwind
63a66,67
> declare void @objc_storeStrong(i8**, i8*)
>

基本上,ARC 和 MRC 之间的唯一区别是添加了一个 objc_storeStrong 调用。


顺便说一下,没有 __strong 限定符的相同代码将产生完全相同的结果,因为 __strong 是 ARC 中变量的默认限定符。

关于ios - __strong 非 ARC 项目中使用的限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609935/

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