加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 是否 – [NSOrderedSet indexesOfObjectsPassing

发布时间:2020-12-16 09:45:30 所属栏目:百科 来源:网络整理
导读:我有这个代码: 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 passin
我有这个代码:

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");

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

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

Return Value
The index of the corresponding value in the ordered set that passes the test specified by predicate. If no objects in the ordered set pass the test,returns NSNotFound..”

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

if(indexes == (NSIndexSet*) NSNotFound) NSLog(@"No objects were found passing the test");

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

解决方法

我担心文档错了.

你应该检查一个空的NSIndexSet:

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

您可能应该与Apple一起打开错误报告.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读