XCode 4.5警告父类/子类的类别之间的方法名称冲突 [英] XCode 4.5 warns about method name conflicts between Categories for parent/child classes

查看:321
本文介绍了XCode 4.5警告父类/子类的类别之间的方法名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个最初在XCode 4.0中构建的项目,然后迁移到使用XCode 4.2。现在我已经测试了迁移到XCode 4.5,我收到了大量警告,如下所示...

I'm working on a project that was originally built in XCode 4.0, and then migrated to using XCode 4.2. Now I've tested out migrating to XCode 4.5, and I'm getting a ton of warnings like below...

instance method 'values' in category from <pathToTheFile>/HistoryObject+extras.o conflicts with same method from another category

这些警告从未出现在以前版本的XCode中,并且代码没有更改。

These warnings never appeared in previous versions of XCode, and the code hasn't changed.

项目在iOS 4.3中设置为部署目标。

The project is set at iOS 4.3 for the Deployment Target.

所以,我们从以前的开发人员那里获得了一堆DAO类型,我认为它们是从CoreData自动生成的,然后这些类中的每一个都有一个类别,它将它扩展到实施某些方法。我将给出一个示例...

So, we have from a previous developer a bunch of DAO type classes that I believe were auto-generated from CoreData, and then each of these classes has a Category that extends it to implement certain methods. I'll give an example...

我们有一个名为LisaObject的基类,它继承自NSManagedObject,它有一个名为LisaObject + extras的类别。在LisaObject + extras中,有一个名为values的方法返回一个NSMutableDictionary。

We have a base class named LisaObject that inherits from NSManagedObject, and it has a Category named LisaObject+extras. In LisaObject+extras, there is a method named "values" that returns an NSMutableDictionary.

然后我们有一个名为HistoryObject的类继承自LisaObject。还有一个名为HistroyObject + extras的HistoryObject类别。此类别还有一个名为值的方法。在HistoryObject + extras values方法中,它调用[超级值],然后检查某些条件并在字典中设置一些未在基类方法中设置的附加值。

We then have a class named HistoryObject that inherits from LisaObject. There is also a Category for HistoryObject that is named HistroyObject+extras. This Category also has a method named "values". In the HistoryObject+extras values method, it calls [super values], and then checks for some conditions and sets some additional values in the dictionary that aren't set in the base class method.

然后我们有一个名为LessonStatusObject的类继承自HistoryObject,它也有一个名为LessonStatusObject + extras的类,它有一个名为values的方法。此值方法还调用[超级值],然后对返回的字典执行一些额外的工作。

We then have a class named LessonStatusObject that inherits from HistoryObject, and it too has a Category named LessonStatusObject+extras, which has a method named values. This values method also calls [super values] and then does some additional work on the returned dictionary.

对于每个值方法,我们在编译时收到警告时间就像上面显示的那样,它说类别有一个名称冲突的方法。

For each of these "values" methods, we get a warning at compile time like the one shown above where it says the Category has a method with a conflicting name.

我有几个问题。

首先,这种实施是否会导致任何合法问题,或者这些警告通常是否良性?我试着想一想这个实现如何在运行时引起歧义,但我不知道怎么会发生这种情况。

First, could this implementation cause any legitimate problems, or are these warnings generally benign? I've tried to think of how this implementation could cause an ambiguity at runtime, but I don't see how that could happen.

第二,我有什么东西吗?应该做些什么来修复这些警告(我并不是说只是让它们停止出现;我的意思是修复原因)?还有其他方法可以解决这个问题吗?

Second, is there something that I should do to fix these warnings (and I don't mean just make them stop appearing; I mean fix the cause)? Is there some other way we should be going about this?

另外,为什么XCode 4.2不会对此发出警告,但XCode 4.5会发出警告?

Also, why would XCode 4.2 not warn about this, but XCode 4.5 does warn?

我是否误解了类别?我的意思是,如果values方法实际上是每个类实现的一部分,那么以我们的方式覆盖它们就不会有问题,但编译器似乎只是因为这些是类别而抱怨。这有什么不安全的吗?

Am I misunderstanding something about Categories? I mean, if the "values" method was actually part of the each class implementation, it wouldn't be a problem to override them the way we do, but the compiler seems to be complaining simply because these are Categories. Is there anything unsafe about this?

非常感谢任何建议。

编辑:仅提供更多信息......当我们使用XCode 4.2时,项目将编译器设置为Apple LLVM Compiler 3.0。现在,当我在XCode 4.5中打开项目时,它将编译器设置为Apple LLVM Compiler 4.1。

Just to provide more information... When we were using XCode 4.2, the project had the compiler set to Apple LLVM Compiler 3.0. Now when I open the project in XCode 4.5, it has the compiler set to Apple LLVM Compiler 4.1.

推荐答案

不要忽略警告。

Apple的使用Objective-C编程指南,在自定义现有类部分说:

Apple's "Programming With Objective-C" guide, in the "Customizing Existing Classes" section, says:


如果在类别中声明的方法的名称与原始类中的方法
相同,或者在同一
类(或甚至是超类)中的另一个类别中的方法相同,关于在运行时使用哪个
方法实现的行为是未定义的。

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.

如果它一直在为你工作,那么这很幸运。

If it has been working for you, then it's luck.

这篇关于XCode 4.5警告父类/子类的类别之间的方法名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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