因为Xcode 4.6不推荐而取代自我&is isa [英] replacing self->isa because of Xcode 4.6 deprecation

查看:112
本文介绍了因为Xcode 4.6不推荐而取代自我&is isa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了Xcode 4.6,现在我在一个古老的代码我管理的错误。

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

编译器现在抱怨直接访问objective-c isa被弃用赞成object_setClass()和object_getClass(),并不会构建。

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];

替换为:

object_setClass(self, [CustomClass class]);

谢谢

推荐答案

访问 isa 已被弃用一段时间,这些工具没有告诉你这一点。值得注意的是,至少只要标签指针存在于obj-c中,它就已经被弃用。

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.

是的, object_setClass c $ 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).

这篇关于因为Xcode 4.6不推荐而取代自我&is isa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆