如何在 clang 中使用 __weak 引用?
How to use __weak reference in clang?
我写了一个简单的OC文件来测试__weak
参考
#import "Foundation/Foundation.h"
@interface Foo : NSObject
@property (nonatomic, assign) int a;
- (void)test;
@end
@implementation Foo
- (void)test
{
__weak typeof(self) weakSelf = self;
[weakSelf test];
}
@end
int main()
{
Foo* foo = [[Foo alloc] init];
foo.a = 3;
[foo test];
return 0;
}
使用 clang -rewrite-objc keke.m
编译时出现以下错误:
cannot create __weak reference because the current deployment target does
not support weak references __attribute__((objc_ownership(weak))) typeof(self) weakSelf = self;
如何在clang
中直接设置depoly目标。我试过了
clang -rewrite-objc -stdlib=libc++ -mmacosx-version-min=10.7 keke.m
但运气不好。
您必须启用并设置运行时版本。尝试:
clang -rewrite-objc -fobjc-arc -stdlib=libc++ -mmacosx-version-min=10.7 -fobjc-runtime=macosx-10.7 -Wno-deprecated-declarations keke.m
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-14.0.0 **.m -o **.cpp
我写了一个简单的OC文件来测试__weak
参考
#import "Foundation/Foundation.h"
@interface Foo : NSObject
@property (nonatomic, assign) int a;
- (void)test;
@end
@implementation Foo
- (void)test
{
__weak typeof(self) weakSelf = self;
[weakSelf test];
}
@end
int main()
{
Foo* foo = [[Foo alloc] init];
foo.a = 3;
[foo test];
return 0;
}
使用 clang -rewrite-objc keke.m
编译时出现以下错误:
cannot create __weak reference because the current deployment target does not support weak references __attribute__((objc_ownership(weak))) typeof(self) weakSelf = self;
如何在clang
中直接设置depoly目标。我试过了
clang -rewrite-objc -stdlib=libc++ -mmacosx-version-min=10.7 keke.m
但运气不好。
您必须启用并设置运行时版本。尝试:
clang -rewrite-objc -fobjc-arc -stdlib=libc++ -mmacosx-version-min=10.7 -fobjc-runtime=macosx-10.7 -Wno-deprecated-declarations keke.m
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-14.0.0 **.m -o **.cpp