从nsarray中检索选择信息

时间:2014-02-26 23:53:34

标签: ios objective-c nsarray nsdictionary

我有一个包含以下词典的数组:

<array>
<dict>
    <key>date</key>
    <string>15/Feb/14</string>
    <key>name</key>
    <string>Victor</string>
    <key>sale</key>
    <string>3</string>
</dict>
<dict>
    <key>date</key>
    <string>21/Feb/14</string>
    <key>name</key>
    <string>Sonny</string>
    <key>sale</key>
    <string>12</string>
</dict>
<dict>
    <key>date</key>
    <string>3/Feb/14</string>
    <key>name</key>
    <string>Mike</string>
    <key>sale</key>
    <string>1</string>
</dict>
<dict>
    <key>date</key>
    <string>02/Jan/14</string>
    <key>name</key>
    <string>Victor</string>
    <key>sale</key>
    <string>6</string>
</dict>
<dict>
    <key>date</key>
    <string>04/Jan/14</string>
    <key>name</key>
    <string>Sonny</string>
    <key>sale</key>
    <string>3</string>
</dict>
<dict>
    <key>date</key>
    <string>02/Jan/14</string>
    <key>name</key>
    <string>Mike</string>
    <key>sale</key>
    <string>8</string>
</dict>
<dict>
    <key>date</key>
    <string>02/Dec/13</string>
    <key>name</key>
    <string>Victor</string>
    <key>sale</key>
    <string>2</string>
</dict>
<dict>
    <key>date</key>
    <string>02/Dec/13</string>
    <key>name</key>
    <string>Sonny</string>
    <key>sale</key>
    <string>8</string>
</dict>
<dict>
    <key>date</key>
    <string>12/Dec/13</string>
    <key>name</key>
    <string>Mike</string>
    <key>sale</key>
    <string>0</string>
</dict>
</array>

我可以使用以下方法轻松检索所有词典的总数:

- (double)totalSales {
    double total = 0.0;

    for (NSDictionary *totalDictionary in salesArray) {
        total += [[totalDictionary objectForKey:@"sale"] doubleValue];    
    }

    return total;
}

但我遇到问题的方法是如何获得以下内容:

  1. 每月(仅限)销售总额(适用于所有人)
  2. 每个人的总销售额(不论日期)。

3 个答案:

答案 0 :(得分:0)

你基本上只需要你正在做的事情,但要限制你所关心的情况。例如,要将其限制为单个人,您可以执行以下操作

- (double)totalSalesForPerson:(NSString *)person {
    double total = 0.0;

    for (NSDictionary *totalDictionary in salesArray) {
            if ([[totalDictionary objectForKey:@"name"] isEqualToString person])
                total += [[totalDictionary objectForKey:@"sale"] doubleValue];    
    }

    return total;
}

尝试对日期做同样的事情。如果您遇到问题,我们可以帮助您。

答案 1 :(得分:0)

要按月获得总销售额,我就是这样做的。我没有测试我的代码,但它应该接近你想要实现的目标。

- (double)totalSalesForMonth:(int)month {
    double total = 0.0;

    for (NSDictionary *totalDictionary in salesArray) {
        NSDateFormatter *timeFormat = [[NSDateFormatter alloc]init];
        timeFormat.dateFormat = [NSString stringWithFormat:@"dd/MMM/yy"];
        NSDate *date = [timeFormat dateFromString:[totalDictionary objectForKey:@"date"]];

        [timeFormat setDateFormat:@"M"];

        if (month == [[timeFormat stringFromDate:date] intValue])
            total += [[totalDictionary objectForKey:@"sale"] doubleValue];
    }

    return total;
}

我还修改了@ ansible的答案,将所有名称转换为小写字符串

- (double)totalSalesForPerson:(NSString *)person {
    double total = 0.0;

    for (NSDictionary *totalDictionary in salesArray) {
        if ([[[totalDictionary objectForKey:@"name"] lowercaseString] isEqualToString:[person lowercaseString]])
            total += [[totalDictionary objectForKey:@"sale"] doubleValue];
    }

    return total;
}

答案 2 :(得分:0)

这是一种不同的方法。假设您呈现的数据位于名为data的数组中:

无论日期如何,都按名称获取总销售额:

// Get a unique set of names
NSSet *names = [NSSet setWithArray:[data valueForKeyPath:@"name"]];

// Run through the names and get the total for each one
for (NSString *currentName in names) {
    // Get all data for the current name
    NSArray *currentNameData = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = %@", currentName]];
    // Add up sales for that name
    NSNumber *currentNameTotal = [currentNameData valueForKeyPath:@"@sum.sale"];
    NSLog(@"%@ total: %@", currentName, currentNameTotal);
}

打印:

2014-02-26 18:21:40.165 Untitled 5[5825:507] Victor total: 11
2014-02-26 18:21:40.166 Untitled 5[5825:507] Mike total: 9
2014-02-26 18:21:40.167 Untitled 5[5825:507] Sonny total: 23

按月计算总数有点困难,因为date字符串不仅仅是月份名称。

// Get a unique set of month names (loop because date contains more than the month name)
NSSet *dateStrings = [NSSet setWithArray:[data valueForKeyPath:@"date"]];
NSMutableSet *monthNames = [NSMutableSet set];
for (NSString *dateString in dateStrings) {
    NSArray *dateStringComponents = [dateString componentsSeparatedByString:@"/"];
    [monthNames addObject:dateStringComponents[1]];
}

使用您的数据,monthNames现在包含DecJanFeb。然后按月使用类似于上述方法的方法按名称查找总计:

for (NSString *monthName in monthNames) {
    // Get all data for dates containing monthName
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date CONTAINS %@", monthName];
    NSArray *currentMonthData = [data filteredArrayUsingPredicate:predicate];
    // Add up sales for that month
    NSNumber *monthTotal = [currentMonthData valueForKeyPath:@"@sum.sale"];
    NSLog(@"Total for %@: %@", monthName, monthTotal);
}

打印:

2014-02-26 18:21:40.169 Untitled 5[5825:507] Total for Dec: 10
2014-02-26 18:21:40.175 Untitled 5[5825:507] Total for Feb: 16
2014-02-26 18:21:40.177 Untitled 5[5825:507] Total for Jan: 17