IOS: 从字典中获取数组中的值
IOS: getting values in an array from dictionary
在我的项目中,我有一个数组,其中包含多个字典,每个字典都有一些键值。我想将 "Link" 键的所有值放入 NSMutableArray
中。
当前包含字典的数组形式为:
(
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
link = "https://player.vimeo.com/video/26186155";
title = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
};
},
{
YoutubeVideo = {
description = "What is endoscopic brain surgery";
link = "https://player.vimeo.com/video/16144037";
title = "What is endoscopic brain surgery";
};
},
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Block Injection pain management";
link = "https://player.vimeo.com/video/26186167";
title = "Cervical Spine Medial Branch Nerve Block Injection pain management";
};
},
{
YoutubeVideo = {
description = "Seguiremos - Hospital Sant Joan de D\U00e9u y Macaco";
link = "https://player.vimeo.com/video/54275902";
title = "Seguiremos - Hospital Sant Joan de D\U00e9u y Macaco";
};
},
{
YoutubeVideo = {
description = "Basic Shoulder Arthroscopy in Orthopaedics";
link = "https://www.youtube.com/embed/ywBVWIzGxtw";
title = "Basic Shoulder Arthroscopy in Orthopaedics";
};
}
我想将 link 的所有值放入一个数组中。
目前我使用的代码给出了以下输出:
-(void)videolisting
{
NSString *post = @"api_key=bf45c093e542f057c123ae7d6";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://hChe/api/video"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"str : %@",str);
NSDictionary *dict6 = [self cleanJsonToObject:responseData];
//NSLog(@"str : %@",dict6);
vidnamearray = [dict6 objectForKey:@"video"];
NSLog(@" Required Dir: %@",vidnamearray);
}
尝试以下解决方案
NSMutableArray *subArray=[[NSMutableArray alloc]init];
for(NSDictionary *dict in vidnamearray)
{
NSDictionary *subDict=[dict valueForKey:@"YoutubeVideo"];
[subArray addObject:[subDict valueForKey:@"link"]];
}
subArray 将获取 link 键中的所有值..!
希望对你有帮助......!
试试这个
yourArr.valueForKeyPath("YoutubeVideo.link")
这将给出包含所有链接的数组。
在我的项目中,我有一个数组,其中包含多个字典,每个字典都有一些键值。我想将 "Link" 键的所有值放入 NSMutableArray
中。
当前包含字典的数组形式为:
(
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
link = "https://player.vimeo.com/video/26186155";
title = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
};
},
{
YoutubeVideo = {
description = "What is endoscopic brain surgery";
link = "https://player.vimeo.com/video/16144037";
title = "What is endoscopic brain surgery";
};
},
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Block Injection pain management";
link = "https://player.vimeo.com/video/26186167";
title = "Cervical Spine Medial Branch Nerve Block Injection pain management";
};
},
{
YoutubeVideo = {
description = "Seguiremos - Hospital Sant Joan de D\U00e9u y Macaco";
link = "https://player.vimeo.com/video/54275902";
title = "Seguiremos - Hospital Sant Joan de D\U00e9u y Macaco";
};
},
{
YoutubeVideo = {
description = "Basic Shoulder Arthroscopy in Orthopaedics";
link = "https://www.youtube.com/embed/ywBVWIzGxtw";
title = "Basic Shoulder Arthroscopy in Orthopaedics";
};
}
我想将 link 的所有值放入一个数组中。 目前我使用的代码给出了以下输出:
-(void)videolisting
{
NSString *post = @"api_key=bf45c093e542f057c123ae7d6";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://hChe/api/video"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"str : %@",str);
NSDictionary *dict6 = [self cleanJsonToObject:responseData];
//NSLog(@"str : %@",dict6);
vidnamearray = [dict6 objectForKey:@"video"];
NSLog(@" Required Dir: %@",vidnamearray);
}
尝试以下解决方案
NSMutableArray *subArray=[[NSMutableArray alloc]init];
for(NSDictionary *dict in vidnamearray)
{
NSDictionary *subDict=[dict valueForKey:@"YoutubeVideo"];
[subArray addObject:[subDict valueForKey:@"link"]];
}
subArray 将获取 link 键中的所有值..!
希望对你有帮助......!
试试这个
yourArr.valueForKeyPath("YoutubeVideo.link")
这将给出包含所有链接的数组。