是 - [NSOrderedSet indexesOfObjectsPassingTest:]真的返回NSIndexSet或NSNotFound吗?

use*_*430 3 cocoa-touch objective-c nsorderedset

我有这个代码:

NSIndexSet *indexes = [anOrderedSet indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        if([self testObj: obj]) return YES;
        else return NO;
    }];

    if(indexes == NSNotFound) NSLog(@"No objects were found passing the test");
Run Code Online (Sandbox Code Playgroud)

这导致Xcode发出警告,指出"指针和整数之间的比较('NSIndexSet*'和'int')".我完全赞同Xcode.

但是,该函数的返回值是类型NSIndexSet*,Apple文档(http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSOrderedSet_Class/Reference/Reference.html)指出:

返回值
通过谓词指定的测试的有序集中对应值的索引.如果有序集中没有对象通过测试,则返回NSNotFound .."

那么当函数的返回类型是指针时,返回NSNotFound(这是一个NSInteger)的处理是NSIndexSet什么?通过将比较更改为以下来禁止警告是否安全:

if(indexes == (NSIndexSet*) NSNotFound) NSLog(@"No objects were found passing the test");
Run Code Online (Sandbox Code Playgroud)

因为从理论上讲,索引可能是一个有效的返回NSIndexSet指针,恰好指向一个等于的内存地址NSNotFound,对吗?

yon*_*ytu 6

我担心文档错了.

你应该检查一个空NSIndexSet:

if ([indexes count] == 0) NSLog(@"No object were found passing the test");
Run Code Online (Sandbox Code Playgroud)

您可能应该向Apple打开一个错误报告.