Writing compare: method in Objective-C

馋奶兔 提交于 2019-12-08 04:50:16

问题


I saw this post: How to sort an NSMutableArray of objects by a member of its class, that is an int or float

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;
    }
}

In my .h file:

NSInteger _type; 
@property NSInteger Type;

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

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


回答1:


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;
    }
}

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.

To explain the whole process:

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.

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.

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.




回答2:


It looks like you've defined Type as an ivar and not a property, therefore it won't be visible in otherObject. Expose it as a property and your comparison should work.



来源:https://stackoverflow.com/questions/6498019/writing-compare-method-in-objective-c

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