replacing self->isa because of Xcode 4.6 deprecation

允我心安 提交于 2020-01-02 04:44:07

问题


I just installed Xcode 4.6 and now I'm getting new errors in an ancient code I manage.

the compiler now complains about "Direct access to objective-c's isa is deprecated in favor of object_setClass() and object_getClass()" and won't build.

so my question is, is this the correct equivalent ?

self->isa = [CustomClass class];

replaced with:

object_setClass(self, [CustomClass class]);

thank you


回答1:


Accessing isa has been deprecated for some time, the tools just didn't tell you this. Notably, it's been deprecated for at least as long as tagged pointers have existed in obj-c.

And yes, object_setClass() is the appropriate replacement.

That said, why do you even need this? It's extremely rare that replacing the class of an object is appropriate, and the only valid case I can think of is when you're trying to dynamically subclass a class in order to inject new behavior into individual instances without modifying the class as a whole (which is, of course, something you probably don't need to do).



来源:https://stackoverflow.com/questions/14590637/replacing-self-isa-because-of-xcode-4-6-deprecation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!