gpt4 book ai didi

ios - 获取唯一对象数组

转载 作者:行者123 更新时间:2023-11-28 19:02:31 26 4
gpt4 key购买 nike

我有一个日期数组:

"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-11 23:00:00 +0000",
"2014-05-11 23:00:00 +0000"

我想把这个数组拆分成两个不同的,放在一个数组中,

我想要的结果:

(
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",

),
(
"2014-05-11 23:00:00 +0000",
"2014-05-11 23:00:00 +0000"
)

这是我尝试过的:

self.tempDatesArray - array of dates (unique)
tempArray - array of all dates

self.tempDatesArray = [[[NSOrderedSet orderedSetWithArray:[self.tempDatesArray copy]] array] mutableCopy]; // i'm getting all the unique dates from array

if (self.tempDatesArray.count > 0){
for (int j=0;j<self.tempDatesArray.count;j++){
if (tempArray[i] == self.tempDatesArray[j]){

[tempArray2 addObject:tempArray[i]]; // temp array

if (j++){

NSLog(@"i is %i",i);

[resultArray addObject:tempArray2];

NSLog(@"test %@",self.sectionsArray);

tempArray2 = [@[] mutableCopy];
}
}
}
}

我的结果是:

(
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-09 23:00:00 +0000",
"2014-05-11 23:00:00 +0000"
),
(
"2014-05-11 23:00:00 +0000"
)

因此我有点沮丧,有人可以指导我正确的方向吗?

最佳答案

另一种方法:

NSMutableArray *result = [NSMutableArray new];
[[arrayOfDates valueForKeyPath:@"@distinctUnionOfObjects.self"] enumerateObjectsUsingBlock:^(NSDate *date, NSUInteger idx, BOOL *stop) {

NSIndexSet *indexSet = [arrayOfDates indexesOfObjectsPassingTest:^BOOL(NSDate *d, NSUInteger idx, BOOL *stop) {
return [d isEqual:date];
}];

[result addObject:[arrayOfDates objectsAtIndexes:indexSet]];
}];

关于ios - 获取唯一对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com