How to compare objects using operator < or > in Objective-C?

柔情痞子 提交于 2019-12-06 04:21:45

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

One way to achieve this is for your class you can define comparisons to everything that make sense to compare with.

@interface MyClass : NSObject
{
    // your stuff
}
- (BOOL)isGreaterThanNumber:(NSNumber *)otherNumber;
+ (BOOL)compare:(MyClass *)myClass toNumber:(NSNumber *)otherNumber;

With well-named functions this isn't terribly different than overloading although yes it prevents you from using > and <. The actual code you write should be identical though as if overloading were included.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!