删除重复的nsarray [英] removing duplicates in nsarray
本文介绍了删除重复的nsarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除nsarray中的重复项。例如我的数组包含以下数据。我想与相邻的日期进行比较,以避免重复,但是通过错误。
任何人都可以指导我我将遇到什么错误
日历首先 -
(
2010-09-25 17:00:00 GMT,
AAA,
2010-09-25 17:00:00 GMT,
AAA,
2010-09-26 17:00:00 GMT,
BBB,
2010-09-26 17:00:00 GMT,
BBB,
2010-09-27 17:00:00 GMT,
CCCC ,
2010-09-27 17:00:00 GMT,
CCC,
2010-09-28 17:00:00 GMT,
AAA
2010-09-28 17:00:00 GMT,
AAA,
2010-09-29 17:00:00 GMT,
DDDD,
2010-09-29 17:00:00 GMT,
DDDD,
2010-09-30 17:00:00 GMT,
BBBB
)
我的代码
code> NSArray日期; // date包含上述值
NSMutableArray * temp_date = [[NSMutableArray alloc] init]; (int i = 0; i< [dates count]; i + = 2)
{
BOOL day;
if([dates count] -2&i){
day = [[dates objectAtIndex:i] compare:[dates objectAtIndex:i + 2]];
}
if(day){
[temp_date addObject:[dates objectAtIndex:i]];
[temp_date addObject:[dates objectAtIndex:i + 1]];
}
}
问候,
sathish
解决方案
你可以使用集合。复制将在创建时自动删除。
NSArray * cleaningArray = [[NSSet setWithArray:dates] allObjects];
请记住一套无序。
根据布伦特的评论,最新的问题解决方案可以让您保持元素的顺序。 [[NSOrderedSet orderedSetWithArray:dates]数组]
how can i remove duplicates in nsarray. for instance my array contains following data. I want to compare with adjacent dates to avoid duplicates but it throughs error.
Can anyone guide me what i am going wrong
calendar first-
(
2010-09-25 17:00:00 GMT,
"AAA",
2010-09-25 17:00:00 GMT,
"AAA",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-27 17:00:00 GMT,
"CCCC",
2010-09-27 17:00:00 GMT,
"CCC",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-30 17:00:00 GMT,
"BBBB"
)
my code
NSArray dates; //dates contain above values
NSMutableArray *temp_date = [[NSMutableArray alloc] init];
for (int i=0; i<[dates count]; i+=2){
BOOL day;
if ([dates count]-2 >i) {
day = [[dates objectAtIndex:i] compare:[dates objectAtIndex:i+2]];
}
if (day) {
[temp_date addObject:[dates objectAtIndex:i]];
[temp_date addObject:[dates objectAtIndex:i+1] ];
}
}
Regards, sathish
解决方案
You could use sets. Duplicates will be removed automatically upon creation.
NSArray *cleanedArray = [[NSSet setWithArray:dates] allObjects];
Bear in mind that a set is unordered.
As per Brent's comment, a more recent solution to the problem that will allow you to keep the elements in order.
[[NSOrderedSet orderedSetWithArray:dates] array]
这篇关于删除重复的nsarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文