由于弃用而替换了自我 [英] replacing self->isa because of deprecation

查看:54
本文介绍了由于弃用而替换了自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了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 complains that "Direct access to objective-c's isa being deprecated in favor of object_setClass() and object_getClass()" and th e project won't build.

所以请告诉我,是:

object_setClass(self, [CustomClass class]);

适当替换:

self->isa = [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()是合适的替换。

也就是说,为什么您甚至需要这个?替换对象的类非常合适,这是极为罕见的,我能想到的唯一有效的情况是,当您试图动态地对一个类进行子类化,以便将新的行为注入单个实例而无需整体修改类时(当然,这可能是您可能不需要执行的操作。

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).

这篇关于由于弃用而替换了自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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