如何在NSMutableArray中对NSMutableDictionary进行小计值

时间:2013-11-24 22:00:29

标签: nsmutablearray xcode5 nsmutabledictionary fast-enumeration subtotal

我知道这更像是一个操作方法,但由于某种原因,我过度复杂应该是一个简单的快速枚举。我只想为每个“数字”小计“移位长度”。我有一个我创建的NSObject将mm:ss转换成秒来轻松转换,我并不担心。我希望结果是NSMutableDictionary的另一个NSArray:

{
Number = 2;
"Shift Length" = "1:30"
}, etc.

提前致谢...

-Paul

样本数据

2013-11-24 15:58:32.677 TimeOnIce[79815:70b] _gameSheetMutableArray: (
    {
    Number = 10;
    "Shift Length" = " 1:32";
    "Time Off Ice" = "08:00";
    "Time On Ice" = "09:32";
},
    {
    Number = 11;
    "Shift Length" = " 1:34";
    "Time Off Ice" = "07:56";
    "Time On Ice" = "09:30";
},
    {
    Number = 14;
    "Shift Length" = " 1:10";
    "Time Off Ice" = "06:56";
    "Time On Ice" = "08:06";
},
    {
    Number = 15;
    "Shift Length" = " 1:10";
    "Time Off Ice" = "06:54";
    "Time On Ice" = "08:04";
},
    {
    Number = 16;
    "Shift Length" = " 1:10";
    "Time Off Ice" = "06:52";
    "Time On Ice" = "08:02";
},
    {
    Number = 2;
    "Shift Length" = " 0:22";
    "Time Off Ice" = "09:38";
    "Time On Ice" = "10:00";
},
    {
    Number = 2;
    "Shift Length" = " 1:08";
    "Time Off Ice" = "06:48";
    "Time On Ice" = "07:56";
},
    {
    Number = 20;
    "Shift Length" = " 1:10";
    "Time Off Ice" = "06:50";
    "Time On Ice" = "08:00";
},
    {
    Number = 3;
    "Shift Length" = " 0:24";
    "Time Off Ice" = "09:36";
    "Time On Ice" = "10:00";
},
    {
    Number = 4;
    "Shift Length" = " 0:26";
    "Time Off Ice" = "09:34";
    "Time On Ice" = "10:00";
},
    {
    Number = 5;
    "Shift Length" = " 0:28";
    "Time Off Ice" = "09:32";
    "Time On Ice" = "10:00";
},
    {
    Number = 6;
    "Shift Length" = " 0:30";
    "Time Off Ice" = "09:30";
    "Time On Ice" = "10:00";
},
    {
    Number = 7;
    "Shift Length" = " 1:32";
    "Time Off Ice" = "08:06";
    "Time On Ice" = "09:38";
},
    {
    Number = 8;
    "Shift Length" = " 1:32";
    "Time Off Ice" = "08:04";
    "Time On Ice" = "09:36";
},
    {
    Number = 9;
    "Shift Length" = " 1:32";
    "Time Off Ice" = "08:02";
    "Time On Ice" = "09:34";
}

修改

我能够通过以下方式解决这个问题:

NSArray *playerNumbersArray = [_gameSheetMutableArray valueForKeyPath:@"@distinctUnionOfObjects.Number"];

    for (NSString *playerNumber in playerNumbersArray)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Number == %@)", playerNumber];
        NSArray *playerNumbersArrayPredicate = [_gameSheetMutableArray filteredArrayUsingPredicate:predicate];

        NSNumber *shiftLength = [playerNumbersArrayPredicate valueForKeyPath:@"@sum.Shift Length"];
        NSLog(@"Player Number %@: Time on Ice: %@", playerNumber, [_convertSecondsToMMSS convertSecondstoMMSS:[shiftLength intValue]]);
    }

1 个答案:

答案 0 :(得分:0)

NSArray *playerNumbersArray = [_gameSheetMutableArray valueForKeyPath:@"@distinctUnionOfObjects.Number"];

    for (NSString *playerNumber in playerNumbersArray)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Number == %@)", playerNumber];
        NSArray *playerNumbersArrayPredicate = [_gameSheetMutableArray filteredArrayUsingPredicate:predicate];

        NSNumber *shiftLength = [playerNumbersArrayPredicate valueForKeyPath:@"@sum.Shift Length"];
        NSLog(@"Player Number %@: Time on Ice: %@", playerNumber, [_convertSecondsToMMSS convertSecondstoMMSS:[shiftLength intValue]]);
}