如何实现“按值分组”在 NSMutableArray 中?
我正在使用 NSMutableArray。我想按日期获取值,就像我们在 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";
}
)
**所以,我刚刚执行了......
NSLog(@"logMuArray %@",[logMuArray valueForKey:@"log_date"]);
但我只想获取唯一的日期。** 我考虑过 NSPredicate 或 Mutable Set 等...
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 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"]);
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"
)
Thanks in advance.....
EDIT:
I have also heared about "@distinctUnionOfObjects"
......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
香蒂的答案很接近。您想要使用键值编码集合运算符
@distinctUnionOfObjects
。将运算符放置在您希望其影响的键之前,就像它是您正在访问的键路径的一部分一样:注意
valueForKeyPath:
,而不是valueForKey:
前者是Key-Value中的方法编码协议,并允许访问任意深度的属性。键路径是由点分隔键组成的NSString
。每个密钥查找的结果依次用于访问下一个密钥(从原始接收者开始);默认情况下,valueForKey:
在每一步都会被简单地调用。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:Notice the use of
valueForKeyPath:
, notvalueForKey:
The former is a method in the Key-Value Coding protocol, and allows accessing arbitrary depth of attributes. The key path is anNSString
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.您应该将
NSSet
用于独特的项目,例如:You should use
NSSet
for UNIQUE items like :您可以使用 KVC 为此。
编辑:编辑乔希的回应
You can use KVC for this.
edit: Editing this wrt Josh's Response
尝试这个逻辑可能对你有帮助
Try this logic might help you
您可以合并一组
这里是一些示例代码
,这里是输出
You can incorporate a set
Here is some example code
and here is the output