如何实现“按值分组”在NSMutableArray? [英] How to implement "group by values" in NSMutableArray?

查看:153
本文介绍了如何实现“按值分组”在NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSMutableArray。我想按日期获取值,就像我们在SQL group中一样按log_date

I am using NSMutableArray. I want to fetch the values by date like we do in SQL group by "log_date".

logMuArray (
        {
        "log_currenttime" = "4:30pm";
        "log_date" = "11.12.2011";
        "log_duration" = "1:30";
    },
        {
        "log_currenttime" = "4:33pm";
        "log_date" = "11.12.2011";
        "log_duration" = "2:21";
    },
        {
        "log_currenttime" = "4:40pm";
        "log_date" = "11.12.2011";
        "log_duration" = "5:30";
    },
        {
        "log_currenttime" = "7:30pm";
        "log_date" = "12.12.2011";
        "log_duration" = "1:30";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "12.12.2011";
        "log_duration" = "2:21";
    },
        {
        "log_currenttime" = "7:40pm";
        "log_date" = "12.12.2011";
        "log_duration" = "5:30";
    },
        {
        "log_currenttime" = "07:16pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:07";
    },
        {
        "log_currenttime" = "7:31pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:04";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:03";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:06";
    },
        {
        "log_currenttime" = "7:35pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:05";
    }
)

**所以,我刚刚执行了...... 。

**So, I have just performed....

 NSLog(@"logMuArray %@",[logMuArray valueForKey:@"log_date"]);

但我只想获取UNIQUE日期。**
我考虑过NSPredicate或可变集等...

But I want to fetch the UNIQUE dates only.** I have thought about NSPredicate or Mutable Set etc...

logMuArray (
    "11.12.2011",
    "11.12.2011",
    "11.12.2011",
    "12.12.2011",
    "12.12.2011",
    "12.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011"
)

提前致谢.....

编辑:

我也听说过@distinctUnionOfObjects

I have also heared about "@distinctUnionOfObjects"

... ...

推荐答案

Shanti的答案很接近。您想使用Key-Value Coding集合运算符 @distinctUnionOfObjects 。将操作员放在您想要影响的钥匙之前,就像它是您正在访问的钥匙路径的一部分一样:

Shanti's answer is close. You want to use the Key-Value Coding collection operator @distinctUnionOfObjects. Place the operator immediately preceding the key which you want it to affect, as if it is a part of the key path you are accessing:

[logMuArray valueForKeyPath:@"@distinctUnionOfObjects.log_date"]

注意使用 valueForKeyPath: ,而不是 valueForKey:前者是键值编码协议中的一种方法,允许任意访问属性深度。关键路径是由点分隔键组成的 NSString 。每个键查找的结果依次用于访问下一个键(从原始接收器开始);默认情况下,只需在每一步调用 valueForKey:

Notice the use of valueForKeyPath:, not valueForKey: The former is a method in the Key-Value Coding protocol, and allows accessing arbitrary depth of attributes. The key path is an NSString made up of dot-separated keys. The result of each key lookup is used in turn to access the next key (starting with the original receiver); by default, valueForKey: is simply called at each step.

这篇关于如何实现“按值分组”在NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆