Reputation: 31
I am trying to add aDict data to myData(NSMutable Array), but am not able to add it. whenever I am trying to print it(myData). It is printing null.
for ( NSDictionary *aDict in responseArray){
NSLog(@"%@", aDict[@"brand"]);
myData[count]=aDict[@"brand"];
}
NSLog(@"%@",myData[2]);
My data is in the format
{
"data": [
{
"brand": "AMANTE",
"brand_id": 216,
"size": "XL",
"size_id": 78,
"url_path": "brands-1.html?brand=216&size=78"
},
{
"brand": "PENNY",
"brand_id": 1349,
"size": "LARGE",
"size_id": 54,
"url_path": "brands-1.html?brand=1349&size=54"
},
{
"brand": "TRIUMPH",
"brand_id": 58,
"size": "LARGE",
"size_id": 54,
"url_path": "brands-1.html?brand=58&size=54"
},
{
"brand": "BENETTON",
"brand_id": 2519,
"size": "LARGE",
"size_id": 54,
"url_path": "brands-1.html?brand=2519&size=54"
},
{
"brand": "HANES",
"brand_id": 446,
"size": "LARGE",
"size_id": 54,
"url_path": "brands-1.html?brand=446&size=54"
},
{
"brand": "JOCKEY",
"brand_id": 60,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=60&size=76"
},
{
"brand": "LOVABLE",
"brand_id": 59,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=59&size=76"
},
{
"brand": "HANES",
"brand_id": 446,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=446&size=76"
},
{
"brand": "ENAMOR",
"brand_id": 56,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=56&size=76"
},
{
"brand": "BIARA",
"brand_id": 2480,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=2480&size=76"
},
{
"brand": "PENNY",
"brand_id": 1349,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=1349&size=76"
},
{
"brand": "TRIUMPH",
"brand_id": 58,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=58&size=76"
},
{
"brand": "LIBERTINA",
"brand_id": 4167,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=4167&size=76"
},
{
"brand": "INNER SENSE",
"brand_id": 3685,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=3685&size=76"
},
{
"brand": "ANITA",
"brand_id": 3919,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=3919&size=76"
},
{
"brand": "ADIRA",
"brand_id": 2584,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=2584&size=76"
},
{
"brand": "COUCOU",
"brand_id": 2008,
"size": "40B",
"size_id": 76,
"url_path": "brands-1.html?brand=2008&size=76"
},
{
"brand": "362436",
"brand_id": 2119,
"size": "XL",
"size_id": 78,
"url_path": "brands-1.html?brand=2119&size=78"
}
],
"status": "success"
}
I want only "brand" in my NSMutable Array *myData.
Upvotes: 1
Views: 66
Reputation: 82759
Try like: if your array of dictionary contains key data
, you can access directly:
NSMutableArray *addarra = [NSMutableArray new];
for ( NSDictionary *aDict in responseArray){
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:aDict[@"brand"] forKey:@"brand"];
[addarra addobject:temp];
}
Retrieve:
NSString *brand = [[addarra objectAtIndex:i]ObjectForKey:@"brand"];
Upvotes: 1
Reputation: 24041
brand
valuesthe easiest way is:
NSArray *_brands = [responseArray[@"data"] valueForKeyPath:@"brand"];
that will gives you an array which has all values, but one brand can be multiple times in the final collection, so if the e.g. TRIUMPH
brands are two times in your original data source, that will be two times in the _brands
array as well.
brand
value only onceyou can do this:
NSArray *_brands = [[NSSet setWithArray:[responseArray[@"data"] valueForKeyPath:@"brand"]] allObjects];
or even that:
NSArray *_brands = [responseArray[@"data"] valueForKeyPath:@"@distinctUnionOfObjects.brand"];
in both case, each brand will be only once in your _brands
array like e.g. TRIUMPH
, although it occurs two times it will be only once in the output collection, regardless how many times the data source has the same value; you can use whichever you'd prefer better or makes more sense to your project.
if you need really a mutable collection at the end:
NSMutableArray *_mutableBrands = [_brands mutableCopy];
Upvotes: 1
Reputation: 478
If the print result is null, it means that you didn't create the "mydata" NSMutableArray. Here is my code.
NSDictionary *dict = @{@"data":@[
@{
@"brand": @"AsafdsadfsdANTE",
@"brand_id": @216,
@"size": @"XL",
@"size_id": @78,
@"url_path": @"brands-1.html?brand=216&size=78"
},@{
@"brand": @"UYTIT",
@"brand_id": @216,
@"size": @"XL",
@"size_id": @78,
@"url_path": @"brands-1.html?brand=216&size=78"
},@{
@"brand": @"sFASDSADF",
@"brand_id": @216,
@"size": @"XL",
@"size_id": @78,
@"url_path": @"brands-1.html?brand=216&size=78"
},@{
@"brand": @"GERWW",
@"brand_id": @216,
@"size": @"XL",
@"size_id": @78,
@"url_path": @"brands-1.html?brand=216&size=78"
},
],
@"status":@"success"
};
NSMutableArray *myData = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
NSArray *data = dict[@"data"];
for (NSDictionary *dict in data) {
NSString *brand = dict[@"brand"];
NSLog(@"%@",brand);
[myData addObject:brand];
}
NSLog(@"%@",myData);
Upvotes: 1