本文介绍了如何找到数组的索引位置其中 NSPredicate 选择值.我使用filteredArrayUsingPredicate作为过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是根据当前时间过滤具有开始时间和结束时间的数组的示例.这工作正常:

here is an example to filter the array which has start time and end time according to the current time. this is working fine:

问题:但在这里我想知道谓词选择值的 collectionArr 的索引位置.

NSDate* currentDate = [NSDate date];
NSDateFormatter *_formatter = [[[NSDateFormatter alloc] init] autorelease];
[_formatter setLocale:[NSLocale currentLocale]];
[_formatter setDateFormat:@"yyyy-MM-dd hh:mm a"];

NSString *_date=[_formatter stringFromDate:currentDate];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"StartDate <= %@ AND EndDate = %@", _date, _date];
if (shortPrograms.count > 0) {
    [shortPrograms removeAllObjects]; 
}

//collectionArr have multiple dictionaries ,which has start date and end date.  
//collectionArr = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"..Sdate..",@"Startdate",@"..Edate..",@"Enddate", nil], nil]; 
//shortPrograms is a filter are, having the date in between start date and end date.

[shortPrograms addObjectsFromArray:[collectionArr filteredArrayUsingPredicate:predicate]];

推荐答案

您的 shortPrograms 数组包含来自 collectionArr 的对象子集.听起来您想从 shortPrograms 获取一个对象并确定它来自 collectionArr 中的何处.

Your shortPrograms array contains a subset of the objects from collectionArr. It sounds like you want to get an object from shortPrograms and determine where in collectionArr it came from.

一般来说你不能.但是如果 collectionArr 中的所有对象都是唯一的,那么这是可能的.

In general you can't. But if all of the objects in collectionArr are unique then it is possible.

id someObject = shortPrograms[someIndex]; // get some value
NSUInteger originalIndex = [collectionArr indexOfObject:someObject];

如果您在 collectionArr 中的对象不是唯一的,那么这将不起作用,因为无法知道您正在处理哪些重复项.

If your objects in collectionArr are not unique, then this won't work because there is no way to know which of the duplicates you are dealing with.

这篇关于如何找到数组的索引位置其中 NSPredicate 选择值.我使用filteredArrayUsingPredicate作为过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 03:05