当前位置:  开发笔记 > 前端 > 正文

iOS:从字典中获取数组中的值

如何解决《iOS:从字典中获取数组中的值》经验,为你挑选了1个好方法。

在我的项目中,我有一个包含多个字典的数组,每个字典都有一些键值。我想将“链接”键的所有值转换为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";
            };
        }

我想将链接的所有值放入单个数组中。目前,我使用的代码给出以下输出:

-(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);

}

Vidhyanand.. 5

尝试以下解决方案

    NSMutableArray *subArray=[[NSMutableArray alloc]init];
    for(NSDictionary *dict in vidnamearray)
    {
        NSDictionary *subDict=[dict valueForKey:@"YoutubeVideo"];
        [subArray addObject:[subDict valueForKey:@"link"]];
    }

subArray将获取链接键中的所有值。

希望它可以帮助您.....!



1> Vidhyanand..:

尝试以下解决方案

    NSMutableArray *subArray=[[NSMutableArray alloc]init];
    for(NSDictionary *dict in vidnamearray)
    {
        NSDictionary *subDict=[dict valueForKey:@"YoutubeVideo"];
        [subArray addObject:[subDict valueForKey:@"link"]];
    }

subArray将获取链接键中的所有值。

希望它可以帮助您.....!

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有