How to compare objects using operator lt; or gt; in Objective-C?(如何使用运算符 lt; 比较对象或gt;在Objective-C中?)
问题描述
如何在 Objective-C 中比较自定义类的两个对象?我尝试重载
How do I compare two objects of a custom class in Objective-C? I try to overloading the
- (NSComparisonResult)compare:(id)other;
方法.如果我手动调用该方法,这将非常有用
method. This works great if I call the method manually
if ([obj1 compare:obj2] == NSOrderedDescending) {
// do something
}
有什么办法可以做到这样吗?
Is there any way that I can do it like this?
if (obj1 > obj2) {
// do something
}
或者我需要重载其他方法吗?
Or is there another method that I need to overload?
推荐答案
这是不可能的,因为 Objective C 没有运算符重载.您正在比较指针值.
This is not possible, since objective C doesn't have operator overloading. You are comparing pointer values.
这篇关于如何使用运算符 < 比较对象或>在Objective-C中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!