object_setClass to bigger class

后端 未结 2 2037
北恋
北恋 2021-02-07 21:28

I am changing the class of some objects using object_setClass(id object, Class cls). I am changing the class to a subclass of the original class. T

相关标签:
2条回答
  • 2021-02-07 22:08

    It could crash. As can be seen in the source code of the runtime here, it really just swaps the isa pointer.

    If you really want to swap the isa to an isa of a subclass with more ivars, you should use class_createInstance with nonzero extraBytes.

    0 讨论(0)
  • 2021-02-07 22:17

    Instead of using a larger subclass, use objc_setAssociatedObject and objc_getAssociatedObject to attach dynamically additional objects to your existing fixed-size object.

    0 讨论(0)
提交回复
热议问题