#import <Foundation/Foundation.h>
extern void _objc_autoreleasePoolPrint();
int main(int argc, char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
Person __weak *p2 = p;
_objc_autoreleasePoolPrint();
}
}
结果是这样的:
$ clang -rewrite-objc -fobjc-arc main.m
/var/folders/t0/nty3j9vx1rn8786q_tpclydc0000gn/T/main-2fe2fd.mi:46509:31: error: cannot create __weak reference
because the current deployment target does not support weak references
Person __attribute__((objc_ownership(weak))) *p2 = p;
^
1 error generated.
求解答
阿神2017-05-02 09:34:47
weak requires support from the runtime environment.
xcrun --show-sdk-path > SDK_PATH;clang -x objective-c -isysroot $SDK_PATH -rewrite-objc -fobjc-arc -fblocks -mios-version-min=8.0.0 -fobjc-runtime=ios-8.0.0 -O0 main.m
When you select a suitable minimum version, you can compile the source code with weak modified variables.
習慣沉默2017-05-02 09:34:47
The parameter rewrite-objc "exports" objc code into c++ code, and __weak is modified when objc ARC is compiled. Do you want to compile or convert?
To convert, change __weak to __unsafe_unretained.
Remove the rewrite-objc parameter when compiling.
$ clang -fobjc-arc main.m -o main.o
$ ./main.o
objc[91465]: ##############
objc[91465]: AUTORELEASE POOLS for thread 0x7fff7c93f000
objc[91465]: 1 releases pending.
objc[91465]: [0x7f8471000000] ................ PAGE (hot) (cold)
objc[91465]: [0x7f8471000038] ################ POOL 0x7f8471000038
objc[91465]: ##############