【问题标题】:Create new arrays based on a NSArray which contains dictionaries基于包含字典的 NSArray 创建新数组
【发布时间】:2012-10-18 11:56:22
【问题描述】:

我有一个字典数组,它来自一个核心数据获取请求,结果类型为NSDictionary

request.resultType = NSDictionaryResultType;
request.returnsDistinctResults = YES;
request.propertiesToFetch = @[@"endCalYear",@"endMonth",@"periodLength"];

NSDictionary 的示例结果 NSArray 如下所示:

{
    endCalYear = 2007;
    endMonth = 12;
    periodLength = 12;
},
{
    endCalYear = 2006;
    endMonth = 9;
    periodLength = 3;
},
{
    endCalYear = 2006;
    endMonth = 6;
    periodLength = 3;
},
{
    endCalYear = 2006;
    endMonth = 3;
    periodLength = 3;
}

创建(三个)单独数组的最有效方法是什么endCalYear = 2006, 2007, 2008; endMonth = 3, 9, 12 和 periodLength = 3, 6, 12 来自给定的 NSArray?

谢谢!

【问题讨论】:

    标签: objective-c ios arrays nsarray nsdictionary


    【解决方案1】:

    你可以使用valueForKey:

    NSArray *endCalYearArray = [resultArray valueForKey:@"endCalYear"];
    

    【讨论】:

    • 谢谢你,马丁!是否可以在不首先创建 NSArray 的情况下访问特定的基于索引的值,例如返回“endCalYear”中的第二项,这将导致 2007 年(即结合 valueForKey 和索引?
    • @AlexR: [[resultArray objectAtIndex:i] objectForKey:@"endCalYear"]
    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2018-01-30
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多