编写比较:Objective-C中的方法 [英] Writing compare: method in Objective-C

查看:64
本文介绍了编写比较:Objective-C中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章:并有类似的内容,但是我不确定它是如何工作的,并希望获得一些指导.我有一个Marker对象数组,这些对象具有一个类型为ivar的值,其值分别为0、1、2.我想要一个复选框,上面写着按0、1或2排序的内容.

And had something similar, but I wasn't sure how it all works and was hoping for some guidance. I have an Array of Marker objects that have a Type ivar that takes on values of 0, 1, 2. I want to have a check box that says what to order by, 0, 1, or 2.

我首先尝试用自己的方法做类似的事情:

I started off by trying to doing something similar in my own method:

- (NSComparisonResult)Type:(id)otherObject {
    if ([self Type] > [otherObject Type]) {
        return NSOrderedAscending;
    }
    else if ([self Type] < [otherObject Type]) {
        return NSOrderedDescending;
    }
    else {
        return NSOrderedSame;
    }
}

在我的.h文件中:

NSInteger _type; 
@property NSInteger Type;

我收到两个警告: 指针和整数之间的有序比较('id'和NSInteger') 方法-找不到类型(返回类型默认为'id')

I get the two warnings: Ordered comparison between pointer and integer ('id' and NSInteger') Method - Type not found (return type defaults to 'id')

当您调用sort方法时,我认为我不明白发生了什么.谢谢.

I don't think I understand what is happening when you call the sort method. Thanks.

推荐答案

如果您明确表示输入类型是YourObjectType *(或对象的任何名称)而不是id,会发生什么情况?为了完全安全起见,您仍然应该检查传入对象的类型(因为它可能根本不响应类型",或者可能会使用相同的选择器返回其他内容),因此您将得到:

What happens if you're explicit about the input type being a YourObjectType * (or whatever your object is called) rather than id? To be completely safe you should check the type of the incoming object anyway (as it may not respond to 'Type' at all, or may return something else with the same selector), so you'd end up with:

- (NSComparisonResult)Type:(YourObjectType *)otherObject {
    if(![otherObject isKindOfClass:[YourObjectType class]]) return NSOrderedSame;

    if ([self Type] > [otherObject Type]) {
        return NSOrderedAscending;
    }
    else if ([self Type] < [otherObject Type]) {
        return NSOrderedDescending;
    }
    else {
        return NSOrderedSame;
    }
}

我基本上是在进行高度咖啡因的猜测,即编译器无法正确确定'Type'的返回类型,但我的猜测是因为编译器对传入对象的预测不正确.

I'm basically making the same guess as highlycaffeinated, that the compiler is failing to correctly determine the return type of 'Type', but my guess is that it's because of an incorrect compiler prediction about the incoming object.

解释整个过程:

当您要求NSArray进行sortUsingSelector时,它将应用某种排序算法,该算法要求它能够比较其中的任何两个对象.当需要比较两个对象时,它将发送您提供的选择器,然后使用返回的结果-因此使用升序/降序,您可以指定如果正确排序,则两个对象出现的顺序.

When you ask NSArray to sortUsingSelector, it'll apply some sorting algorithm that requires it to be able to compare any two objects within it. When it needs to compare two objects, it'll send the selector you supplied, then use the result you return — so with Ascending/Descending you're specifying the order that the two objects would appear in if correctly sorted.

例如,如果您的NSArray填充了SillyObjectType类型的对象,并且发出了sortUsingSelector:@selector(compare:),则NSArray将应用一些未指定的排序算法,该算法涉及将选择器compare:发送到SillyObjectType的实例.每个SillyObjectType都被问到您应该在另一个对象之前,之后还是在另一个位置?".它回复为NSOrderedAscending,Descending或Same.

For example, if you had an NSArray filled with objects of type SillyObjectType and you issued a sortUsingSelector:@selector(compare:) then NSArray would apply some unspecified sorting algorithm that involved sending the selector compare: to instances of SillyObjectType. Each SillyObjectType is being asked "should you come before, after, or at an equal place to this other object?". It replies with NSOrderedAscending, Descending or Same.

在这个地方,ObjC的动态调度提供的东西编写方式与您在例举中使用的方式略有不同. C ++.选择器将被发送到数组中的对象,因此您应该在这些对象上实现选择器.在每种情况下都适用正常规则,因此self将是该类的实例.

This is one place where ObjC's dynamic dispatch gives a slightly different way of writing the thing than you'd use in e.g. C++. The selector will be sent to the objects in the array, so you should implement it on those objects. In each case the normal rules apply, so self will be that instance of that class.

这篇关于编写比较:Objective-C中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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