我正在使用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"]);
但我只想获取UNIQUE日期。** 我考虑过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”
...
答案 0 :(得分:14)
Shanti的答案很接近。您想要使用键值编码集合运算符@distinctUnionOfObjects
。将操作员放在您想要影响的密钥之前,就像它是您正在访问的密钥路径的一部分一样:
[logMuArray valueForKeyPath:@"@distinctUnionOfObjects.log_date"]
注意valueForKeyPath:
的使用,而不是valueForKey:
前者是键值编码协议中的一种方法,允许访问任意深度的属性。关键路径是由点分隔键组成的NSString
。每个键查找的结果依次用于访问下一个键(从原始接收器开始);默认情况下,只需在每一步调用valueForKey:
。
答案 1 :(得分:3)
您可以使用KVC。
[logMuArray valueForKey:@"@distinctUnionOfArrays.log_date"]
编辑:编辑此作为乔希的回应
[logMuArray valueForKeyPath:@"@distinctUnionOfArrays.log_date"]
答案 2 :(得分:3)
您应该将NSSet
用于UNIQUE项目,例如:
NSSet *filteredData = [NSSet setWithArray:[logMuArray valueForKey:@"log_date"]];
答案 3 :(得分:1)
试试这个逻辑可能对你有帮助
-(NSMutableArray *) makeUnique :(NSMutableArray *) array {
int i;
int count = [array count];
for (i =0; i< count ; i++) {
NSRange range = NSMakeRange (i+1, count);
[array removeObject:[array objectAtIndex:i] inRange:range];
}
return array;
}
答案 4 :(得分:0)
您可以合并一套
以下是一些示例代码
NSMutableArray * mArray = [NSMutableArray array];
NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys:@"foo",@"bar",@"bar",@"oooo",nil];
NSDictionary *d2 = [NSDictionary dictionaryWithObjectsAndKeys:@"boo",@"bar",@"bar",@"oooo",nil];
NSDictionary *d3 = [NSDictionary dictionaryWithObjectsAndKeys:@"boo",@"bar",@"bar",@"oooo",nil];
[mArray addObject:d1];
[mArray addObject:d2];
[mArray addObject:d3];
NSLog(@"the array\n%@", mArray);
NSLog(@"just bar %@", [mArray valueForKey:@"bar"]);
//get unique values
NSSet * set = [NSSet setWithArray:[mArray valueForKey:@"bar"]];
NSLog(@"unique just bar %@", [set allObjects]);
这是输出
2011-12-20 01:50:59.034 TestEnvironment[32401:207] the array
(
{
bar = foo;
oooo = bar;
},
{
bar = boo;
oooo = bar;
},
{
bar = boo;
oooo = bar;
}
)
2011-12-20 01:50:59.036 TestEnvironment[32401:207] just bar (
foo,
boo,
boo
)
2011-12-20 01:50:59.038 TestEnvironment[32401:207] unique just bar (
foo,
boo
)