如何使用运算符 < 比较对象或>在Objective-C中?

How to compare objects using operator lt; or gt; in Objective-C?(如何使用运算符 lt; 比较对象或gt;在Objective-C中?)
本文介绍了如何使用运算符 < 比较对象或>在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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android Paging 3 - get list of data from PagingDatalt;Tgt; object(Android分页3-从PagingDatalt;Tgt;对象获取数据列表)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
NSURLSessionTaskPriority seems to be ignored?(NSURLSessionTaskPriority似乎被忽略了?)
How to make dataWithEPSInsideRect vector rather than bitmap in vector format?(如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式?)