Reputation: 622
I am using core data to retrieve a transferrable object which is then saved as an NSArray as follows:
NSArray *bigDataResultsArray = [XAppDelegate.managedObjectContext executeFetchRequest:bigDataFetchRequest error:&error];
NSArray *bigDataResultsArrayJSON = [bigDataResultsArray valueForKey:@"bigData"];
I am able to get valueForKey down to a smaller object array however it is only appearing as 1 object.
When I try to extract "Value" from the following I get the error that it is non value compliant. The data is in the following format and will not parse to JSON:
I try to extract value using the following:
NSArray *styleArray = [bigDataResultsArrayJSON valueForKey:@"Styles"];
NSLog(@"%@", [styleArray valueForKey:@"Value"]);
Which gives an error. However if i log the style array I get the following"
(
{
Total = 1;
Value = 5;
},
{
Total = 1;
Value = "5.5";
},
{
Total = 75;
Value = 6;
},
{
Total = 4;
Value = "6.5";
},
{
Total = 239;
Value = 7;
},
{
Total = 149;
Value = "7.5";
},
{
Total = 260;
Value = 8;
},
{
Total = 214;
Value = "8.5";
},
{
Total = 259;
Value = 9;
},
{
Total = 196;
Value = "9.5";
},
{
Total = 254;
Value = 10;
},
{
Total = 148;
Value = "10.5";
},
{
Total = 237;
Value = 11;
},
{
Total = 38;
Value = 12;
},
{
Total = 2;
Value = 13;
}
)
)
Can anyone assist me with extracting the final layer of this data into an array of value objects. I have tried converting to NSData then to JSON as follows but the format of the data will not parse:
NSData *raw = [[ra valueForKey:@"bigData"] dataUsingEncoding:NSUTF8StringEncoding]
Any help greatly appreciated.
Upvotes: 0
Views: 580
Reputation: 1844
Your styleArray is an array of dictionaries. Arrays do not have key-value pairs; each element of the array does. What Mundi is saying is the correct answer, and will give you a single array of values.
To be super explicit, you can loop through the elements of styleArray, and extract each dictionary individually, and maybe this will be more clear to you.
NSArray *styleArray = [bigDataResultsArrayJSON valueForKey:@"Styles"];
NSMutableArray *valuesArray = [NSMutableArray array];
for (int i=0; i<styleArray.count; i++) {
NSDictionary *styleDictionary = styleArray[i];
id valueObj = [styleDictionary objectForKey:@"Value"];
[valuesArray addObject:valueObj];
}
NSLog(@"Values: %@", valuesArray);
Upvotes: 0
Reputation: 80265
You can extract an array with just one field from an array of dictionaries:
NSArray *dictArray = @[{@"total" : @1, @"value" : @5},
{@"Total" : @1, @"Value" : @5.5}, … ];
NSArray *resultsArray = [dictArray valueForKeyPath:@"value"];
---> @[@5, @5.5, …];
You can do the same with an array of core data objects:
// ... create the managed objects and fill their attributes
object.total = @1;
object.value = @5;
// etc
NSArray *resultsArray = [fetchedObjects valueForKeyPath:@"value"];
---> @[@5, @5.5, …];
Upvotes: 1