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 ). :)

user1028028
  • 6,323
  • 9
  • 34
  • 59
  • Do you have some additional info regarding the dates&numbers array? For instance: Is it the same array that is used in each iteration? can you manipulate it and add an additional property to each object? Etc. – Michael Kessler Nov 18 '12 at 21:01
  • it is the same, it will not change, but different subarrays of different split intervals (months, days, years) will be needed from it – user1028028 Nov 18 '12 at 23:04
  • Oh ... well yes ... I could add a new property to each object if needed ... – user1028028 Nov 19 '12 at 00:26
  • The second problem can probably only be handled by iterating through the array (use fast enumeration) and processing it yourself. Since performance is an issue, you should do this once and cache the results (using a delayed getter). I don't have time to write an example right now but hopefully this will get you started! – lnafziger Nov 19 '12 at 00:46

1 Answers1

1

The first issue may be solved by using an NSPredicate.
Let's say, if the second NSNumber property in the objects is called secondNumber:

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

Hope it compiles - I wrote it without XCode.

The second issue sounds a bit harder.
Let me think about it...

Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • For the second problem I would suggest to redesign the implementation if possible. For instance, create another object that will have one property for month and another property of NSMutableArray kind. The mutable array would hold all the objects for that month. This way you would iterate through it much easier. – Michael Kessler Nov 19 '12 at 10:09
  • So once the initial array is provided I'll just calculate the other arrays that contain the month sums, and other interval split sizes ... that could work .. i guess – user1028028 Nov 19 '12 at 12:49