• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS无法将字典值获取到数组

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:16:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有字典:

{
6 =     (
            {
        id = 6;
        name = Andrea;
    },
            {
        id = 6;
        name = Paolo;
    }
);
8 =     (
            {
        id = 8;
        name = Maria;
    }
);
67 =     (
            {
        id = 67;
        name = Francesco;
    },
            {
        id = 67;
        name = Sara;
    }
);
}

我试图将值放入数组中。

我的代码是:

 NSArray *arrayNew= [result valueForKey"67"];

NSLog(@"Hello:%@",arrayNew);

但我总是得到空值。

我的完整代码:

   NSMutableArray *idArray=[[NSMutableArray alloc]init];


NSString* path = [[NSBundle mainBundle] pathForResource"File"
                                                 ofType"txt"];

NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

NSMutableData *results11 = [content JSONValue];

NSLog(@"Check:%@",results11);

NSArray *array= [results11 valueForKeyPath"field"];





NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (NSDictionary* dict in array)
{
    NSNumber* anID = [dict objectForKey"id"];


    if (![idArray containsObject:[dict objectForKey"id"]]) {
        // do something

         [idArray addObject:[dict objectForKey"id"]];

    }






    NSMutableArray* resultsForID = [result objectForKey:anID];

    if (!resultsForID)
    {
        resultsForID = [NSMutableArray array];
        [result setObject:resultsForID forKey:anID];
    }

    [resultsForID addObject:dict];


}

NSLog(@"Result:%@",result);
NSLog(@"ID arr:%@",idArray);

 //   NSArray *temp= [results11 valueForKeyPath"Response.alertHistory"];




NSString *arrayNew= [result valueForKeyPath"67"];

NSLog(@"Hello:%@",arrayNew);

文件.txt : { “ field ”: [ { “身份证”:6, “名称”:“安德烈亚” }, { “身份证”:67, “名称”:“弗朗西斯科” }, { “身份证”:8, “名字”:“玛丽亚” }, { “身份证”:6, “名称”:“保罗” }, { “身份证”:67, “名称”:“萨拉” } ] }



Best Answer-推荐答案


最后引用 Tommy 的解决方案,我解决了我的问题。

NSArray *commentArray = result[@67];

还有我的最终代码:

 NSMutableArray *idArray=[[NSMutableArray alloc]init];


NSString* path = [[NSBundle mainBundle] pathForResource"File"
                                                 ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

NSMutableData *results11 = [content JSONValue];

NSLog(@"Check:%@",results11);

NSArray *array= [results11 valueForKeyPath:@"field"];





NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (NSDictionary* dict in array)
{
    NSNumber* anID = [dict objectForKey:@"id"];


    if (![idArray containsObject:[dict objectForKey:@"id"]]) {
        // do something

         [idArray addObject:[dict objectForKey:@"id"]];

    }






    NSMutableArray* resultsForID = [result objectForKey:anID];

    if (!resultsForID)
    {
        resultsForID = [NSMutableArray array];
        [result setObject:resultsForID forKey:anID];
    }

    [resultsForID addObject:dict];


}

NSLog(@"Result:%@",result);





NSMutableArray *arrayNew = [[NSMutableArray alloc] init];




for (int i=0; i<[idArray count]; i++) {

    NSArray *commentArray = result[idArray[i]];


    NSLog(@"COMM:%@",commentArray);


    NSMutableArray *arr=[[NSMutableArray alloc]init];

    for (NSDictionary* dict in commentArray)
    {
        [arr addObject:[dict objectForKey:@"name"]];
    }


    [arrayNew addObject:arr];
}


NSLog(@"ID arr:%@",idArray);
NSLog(@"Name arr:%@",arrayNew);

文件.txt:

{
"field": [
{
"id": 6,
"name": "Andrea"
},
{
"id": 67,
"name": "Francesco"
},
{
"id": 8,
"name": "Maria"
},
{
"id": 6,
"name": "aolo"
},
{
"id": 67,
"name": "Sara"
}
]
}

我的最终结果:

ID arr
6,
67,
8
)

 Name arr
        (
        Andrea,
        Paolo
    ),
        (
        Francesco,
        Sara
    ),
        (
        Maria
    )
)

关于iOS无法将字典值获取到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358787/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap