如何找到疏忽的对象指针比较? [英] How to find inadvertent object pointer comparisons?

查看:116
本文介绍了如何找到疏忽的对象指针比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问-是否有一种很好的方法来找到对象使用==而不是isEqual:的用途?

Quick question - Is there a good way to find uses of == with objects instead of isEqual:?

完整的故事:

我有一堆这样的代码:

typedef long DataKey;

DataKey x;
DataKey y;

if (x == y) {
    // do stuff
}

我现在需要将DataKeylong替换为对象.创建完类并进行了一系列全局搜索和替换之后,我的代码现在就像这样:

I now have a need to replace using a long for my DataKey with an object. After creating the class and doing a bunch of global search and replace, my code now is kind of like this:

@interface DataKey : NSObject

DataKey *x;
DataKey *y;

if (x == y) { // uh-oh - this is now bad
    // do stuff
}

是否可以在编译器中启用警告以警告在指针中使用标量运算符?我正在将Xcode 4.5.2与LLVM 4.1编译器一起使用.我找不到一个.

Is there a warning I can enable in the compiler that warns about using the scalar operators in pointers? I'm using Xcode 4.5.2 with the LLVM 4.1 compiler. I haven't been able to find one.

还有其他建议可以解决所有这些代码吗?这不是一个简单的代码库.有数百个源文件要处理.这是一项重大的重构工作.

Any other suggestions to help fix all of this code? This is not a trivial code base. There are hundreds of source files to deal with. This is a major refactoring effort.

如果有一个警告,就像在if条件下使用赋值而不是比较时那样,那将是很棒的.您可以为此获得警告,并使用括号将警告停止.

It would be great if there was a warning much like when you use assignment in an if condition instead of comparison. You can get a warning for this and use parenthesis to stop the warning.

更新:

基于注释中的建议,我在测试项目中添加了-Weverything编译器选项.将两个对象指针与==进行比较时,这不会产生任何期望的结果.

Based on a suggestion in the comments, I added the -Weverything compiler option to a test project. This did not produce any desired result when comparing two object pointers with ==.

也许可以结合使用C ++,重载operator==方法并在尝试使用该运算符时得到一些编译器警告/错误.我可能会发布另一个针对此选项的问题.

Perhaps a combination of C++, overloading the operator== method, and getting some compiler warning/error when trying to use that operator can be achieved. I may post another question focused on this option.

推荐答案

我很确定没有这样的警告,因为比较相等性的指针非常普遍,而且并非所有错误均会发生.不幸的是,最好的选择是遍历任何可能以这种方式使用DataKeys并搜索==的地方.我知道不是很有趣.

I'm pretty sure there is no such warning, as comparing pointers for equality is pretty common and not all that commonly done in error. Your best bet will unfortunately be to go through any place DataKeys might be used this way and search for ==. Not very fun, I know.

这篇关于如何找到疏忽的对象指针比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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