I have an array that has the past 5 days. It is built like this:
(
"2015-01-27",
"2015-01-26",
"2015-01-25",
"2015-01-24",
"2015-01-23",
)
I have a second NSArray from a FetchRequest
(
{
daySectionIdentifier = "2015-01-24";
sumValue = 2500;
},
{
daySectionIdentifier = "2015-01-25";
sumValue = 1487;
},
{
daySectionIdentifier = "2015-01-27";
sumValue = 750;
}
)
What I want is the dates that match my first array get a value in the first array, the missing dates get no value.
So the final result will look like this:
(
{
daySectionIdentifier = "2015-01-23";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-24";
sumValue = 2500;
},
{
daySectionIdentifier = "2015-01-25";
sumValue = 1000;
},
{
daySectionIdentifier = "2015-01-26";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-27";
sumValue = 750;
}
)
Anybody have an idea how to do this? Thanks in advance
Ok so this didn't turn out to be too hard, hopefully this is what you are after:
Firstly thinking about the problem, the issue somewhat here is getting the data in the right format to be able to analyse, so first of all I changed it from an array filled with dictionaries to an array of arrays with each array containing the information (I know not the most elegant solution but one that works none the less)
// Here is our array of past dates
NSArray * pastDateDays = @[@"2015-01-27", @"2015-01-26", @"2015-01-25", @"2015-01-24", @"2015-01-23"];
// Here is our array from the request, this is full of dictionaries
NSArray * fetchRequest = @[@{@"daySectionIdentifier" : @"2015-01-24", @"sumValue": @2500}, @{@"daySectionIdentifier" : @"2015-01-25", @"sumValue": @1487}, @{@"daySectionIdentifier" : @"2015-01-27", @"sumValue": @750}];
// Here is a mutable array we will be adding to
NSMutableArray * request = [NSMutableArray arrayWithArray:fetchRequest];
So now we are ready to start getting the information into a slightly nicer format.
// This function gets the array in an array of arrays where each array has a date and a value
fetchRequest = [self fetchRequestToArray:fetchRequest];
// Not too complicated just taking it out of one and putting it in another
- (NSArray *)fetchRequestToArray: (NSArray *)array {
NSMutableArray * tempArray = [NSMutableArray new];
for (NSDictionary * dict in array) {
NSArray * temp = @[[dict objectForKey:@"daySectionIdentifier"], [dict objectForKey:@"sumValue"]];
[tempArray addObject:temp];
}
return [NSArray arrayWithArray:tempArray];
}
Next we loop through a mutable array of the dates in our date array and if they match in our requested array we remove them:
NSMutableArray * tempDates = [NSMutableArray arrayWithArray:pastDateDays];
for (NSArray * array in fetchRequest) {
NSString * date = array.firstObject;
for (NSString * string in pastDateDays) {
if ([date isEqualToString:string]) {
[tempDates removeObject:string];
}
}
}
This leaves us with an array of dates which are included in our date array but are not included in our requested data. These are the dates we need to add a zero value for.
Again this is relatively simple:
for (NSString * date in tempDates) {
NSDictionary * dict = [NSDictionary dictionaryWithObjects:@[date, @0]
forKeys:@[@"daySectionIdentifier", @"sumValue"]];
[request addObject:dict];
}
This returns us with the desired array.
The only thing that might need to be added is that this array isn't in date order. This can be easily sorted with a number of methods. I found and added this on in a few seconds but you could choose a more complicated one if you need it:
NSSortDescriptor * sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"daySectionIdentifier"
ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortByDate];
NSArray * sortedArray = [request sortedArrayUsingDescriptors:sortDescriptors];
This will output the date in the format:
The final array is the array called request and is a mutableArray
<__NSArrayI 0x7fade848f480>(
{
daySectionIdentifier = "2015-01-23";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-24";
sumValue = 2500;
},
{
daySectionIdentifier = "2015-01-25";
sumValue = 1487;
},
{
daySectionIdentifier = "2015-01-26";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-27";
sumValue = 750;
}
)
Which I think is the desired output.
Things to note: - The values are NSNumbers and not integers as we can't store integers in an NSdictionary
This is not the most elegant solution - I have used a lot of arrays and i am sure it could be refactored. This code though does work and so can be worked with to build understanding - this code should work when copied straight in but there may be a few things needing tweaking as it is a long answer copied from my XCode
The strings need to be in exactly this format for it to work, if they are not then this solution will need to be tweaked.
I hope this helps