iOS-OC对象的本质

231 阅读1分钟

OC对象经过编译后在底层是一个结构体 struct。

使用clang命令可以将我们的OC源文件转译成cpp文件。

clang -rewrite-objc main.m -o main.cpp

以下为转译后的部分代码

发现转译后的LGPerson为objc_object类型,查看Runtime源码,objc_object为带有isa的struct类型。

// objc_private.h
struct objc_object {
private:
    isa_t isa;

public:

    // ISA() assumes this is NOT a tagged pointer object
    Class ISA();

    // getIsa() allows this to be a tagged pointer object
    Class getIsa();
    ......

由此可知,OC对象的本质为struct。

clang其他命令: 解决存在UIKit系统其他动态库引用问题:

clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m

xcrun xcode命令:

xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m

xcrun -sdk iphones clang -rewrite-objc ViewController.m