如何使用operator&或>在Objective-C? [英] How to compare objects using operator < or > in Objective-C?

查看:169
本文介绍了如何使用operator&或>在Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较Objective-C中自定义类的两个对象?我尝试重载



- (NSComparisonResult)compare:(id)other;



方法。这个工作很好,如果我手动调用方法

  if([obj1 compare:obj2] == NSOrderedDescending){
// do something
}

有什么办法可以这样做吗?

  if(obj1> obj2){
// do something
}



还是有其他方法需要重载?

解决方案

这是不可能的,因为目标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?

解决方案

This is not possible, since objective C doesn't have operator overloading. You are comparing pointer values.

这篇关于如何使用operator&或>在Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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