如何使用运算符 < 比较对象或>在Objective-C中? [英] How to compare objects using operator < or > in Objective-C?

查看:25
本文介绍了如何使用运算符 < 比较对象或>在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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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