0

So, I think this won't be easy ... Here goes: I have two NSArrays , each contain some custom objects (KVC compliant .. maybe this will help). The objects have two properties in the first array (lets call it array1) this objects are NSNumbers, in the second array one object is a NSNumber the other is a NSDate... Here is the 2-part question:

1) How do I get an NSArray from array 1 with all the objects that have one of the NSNumbers in a specific range

lets say the array is [(1,1),(2,4),(2,12), (3,14)] and I need an array that has the second value between 3 and 13, (only object 2 and 3 in this case) .. And yes the array is ordered on the second variable ...

2) In the second array I need objects that have the date in an interval, BUT I need to sub-split this interval in smaller intervals and the returned array will have this new objects that have the sum of the numeric objects from the first interval .... An example is in order in case I have not been clear ....

The initial array is [(2,January 12),(5,January 13),(20,march 15),(10, march 20),(26,April 28),(20,may 6),(30,july 4)]; And I need interval february 1 - may 30 , split into months, the result should be [(0,february),(30,march (20+10 from the initial array)),(26,april),(30,may)];

This initial date-number array will not change... and the subinterval split time frames will allways be : from the begining of the interval to the end of the last interval ... (from the first day (midnight) of january to the last day in march , or from the first second of 1:00 PM to 3:59:59.999 PM)

It would be wonderful if the solution is efficient, as these array might have up to 10,000 objects... and this getRangeWithInterval function may be called more than once a second (and should run on iPhone 3GS ... I hope I'm not asking for too much ). :)

4

1 回答 1

1

第一个问题可以通过使用 NSPredicate 来解决。
假设,如果对象中的第二个 NSNumber 属性被调用secondNumber

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"secondNumber >= 3 && secondNumber <= 13"];
NSArray *filteredArray = [array1 filteredArrayUsingPredicate:predicate];

希望它可以编译——我在没有 XCode 的情况下编写了它。

第二个问题听起来有点难。
让我想想...

于 2012-11-18T20:46:04.320 回答