从数组中获取变量
get var from an array
使用该代码我能够获取数组的所有列表。
看了很多教程,但如果 "name" 等于 "Timeline Photos",我无法获得名为 "id" 的变量。
代码:
NSArray *feed =[result objectForKey:@"data"];
for (NSDictionary *dict in feed) {
NSLog(@"%@",dict);
}
结果(前两个结果的例子):
2015-02-03 13:59:21.246 Project[708:29363] {
"can_upload" = 0;
count = 68;
"cover_photo" = 10200808394440000;
"created_time" = "2011-03-09T17:25:03+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1716972770000;
link = "https://www.facebook.com/album.php?..";
name = "Mobile Uploads";
privacy = everyone;
type = mobile;
"updated_time" = "2015-01-31T13:28:32+0000";
}
2015-02-03 13:59:21.247 Project[708:29363] {
"can_upload" = 0;
count = 11;
"cover_photo" = 4383404270000;
"created_time" = "2010-02-15T15:50:30+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1267183610000;
link = "https://www.facebook.com/album.php?...";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2015-01-28T18:26:52+0000";
}
试试这个:
NSArray *feed =[result objectForKey:@"data"];
for (NSDictionary *dict in feed) {
if ([dict[@"name"] isEqualToString:@"Timeline Photos"])
NSLog(@"%@",dict[@"id"]);
}
这只会在 name
等于时间轴照片时打印 id
。
实际上,您不是在使用数组,而是在使用字典。
希望对您有所帮助。
使用该代码我能够获取数组的所有列表。
看了很多教程,但如果 "name" 等于 "Timeline Photos",我无法获得名为 "id" 的变量。
代码:
NSArray *feed =[result objectForKey:@"data"];
for (NSDictionary *dict in feed) {
NSLog(@"%@",dict);
}
结果(前两个结果的例子):
2015-02-03 13:59:21.246 Project[708:29363] {
"can_upload" = 0;
count = 68;
"cover_photo" = 10200808394440000;
"created_time" = "2011-03-09T17:25:03+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1716972770000;
link = "https://www.facebook.com/album.php?..";
name = "Mobile Uploads";
privacy = everyone;
type = mobile;
"updated_time" = "2015-01-31T13:28:32+0000";
}
2015-02-03 13:59:21.247 Project[708:29363] {
"can_upload" = 0;
count = 11;
"cover_photo" = 4383404270000;
"created_time" = "2010-02-15T15:50:30+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1267183610000;
link = "https://www.facebook.com/album.php?...";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2015-01-28T18:26:52+0000";
}
试试这个:
NSArray *feed =[result objectForKey:@"data"];
for (NSDictionary *dict in feed) {
if ([dict[@"name"] isEqualToString:@"Timeline Photos"])
NSLog(@"%@",dict[@"id"]);
}
这只会在 name
等于时间轴照片时打印 id
。
实际上,您不是在使用数组,而是在使用字典。
希望对您有所帮助。