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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用运算符 < 比较对象或>在Objective-C中?


基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01