NSCountedSet - 获取集合中的项目数

时间:2011-09-30 03:02:48

标签: iphone objective-c ios ipad nsset

我有一个信息数组,其中包含多个我想要匹配的名称,然后计数然后移动到表格视图,就像您在消息应用中看到的那样。

我已达到这一点。在哪里我可以记录并查看我想要的内容,但根本无法找到任何解决方案从该集中提取该信息并相应地放入 tableview

示例

- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString    
*)connectionIdentifier{

for(NSDictionary *d in messages) {

  // NSLog(@"See Direct Messageges Parsing Info: %@", d);

    Tweet *tweetDic = [[Tweet alloc] initWithTweetDictionary:d];


    //[directMessages addObject:tweetDic];
    [tweetDic autorelease];



}

//[arayToScan addObject:[messages valueForKey:@"sender_screen_name"]];
[arayToScan addObjectsFromArray:messages];
[self checkForDuplicateIDs]; 
// NSLog(@"Same ScreenName Received: %@", arayToScan);

//[self.mainTableView reloadData];    
 NSLog(@"DM COUNT = %i",[directMessages count]);

[DSBezelActivityView removeViewAnimated:YES];


}



- (NSSet *)checkForDuplicateIDs {

//CHECK FOR DUPLICATES IN ARRAY
NSArray *allIDs = [arayToScan valueForKeyPath:@"sender_screen_name"];
NSArray *sortedIDs = [allIDs sortedArrayUsingSelector:@selector(compare:)];

NSString *previousID = nil;
duplicateIDs = [NSMutableSet set];
for (NSString *anID in sortedIDs) {
    if ([previousID isEqualToString:anID]) {
        [duplicateIDs addObject:anID];

    }
    previousID = anID;
}


//THEN REMOVE ANY DUPLICATES IN NEW ARRAY
NSCountedSet *countedSet = [NSCountedSet setWithArray:sortedIDs];
NSMutableArray *oneMess = [NSMutableArray arrayWithCapacity:[sortedIDs count]];
NSMutableArray *multipleMess = [NSMutableArray arrayWithCapacity:[sortedIDs count]];


for(id obj in countedSet) {
    if([countedSet countForObject:obj] == 1) {
        [oneMess addObject:obj];
    }
 for(id duplicatenames in countedSet) {
     if ([countedSet countForObject:duplicatenames]>1) {
         [multipleMess addObject:duplicatenames];
         [countedSet  countForObject:duplicatenames];

     }


}


}
//NSLog(@"NSCountedSet = %@",[countedSet allObjects]);
 //NSLog(@"NSCountedSet = %@",countedSet);
//NSLog(@"One Message = %@",oneMess);
// NSLog(@"Multiple Messages = %@",multipleMess);


//HERE I HAVE ORIGINAL ARRAY IN ALBETICAL ORDER
// NSLog(@"Messages Containing more then 1 = %@",sortedIDs);    

//HERE I HAVE DUPLICATES IN ALBETICAL ORDER
// NSLog(@"Messages Containing more then 1 = %@",duplicateIDs);

//HERE I HAVE THE MESSAGES ONLY CONTAING 1
//NSLog(@"Messages Containing only 1 = %@",oneMess);


//NOW I WANT TO COUNT THE DUPLICATES TO PLACE AS A STRING IN CELL FOR # OF MESSAGES & #   
OF ROW IN TBLVIEW
//NSLog(@"Duplicates count = %i",[duplicateIDs count]);
// NSLog(@"Messages Containing only count = %i",[oneMess count]);

//HERE I WOULD ADD BOTH ARRAYS AND SEND TO MY ARRAY BELOW WHICH IS USED FOR MY TBLVIEW
THIS INFO IS RETRIEVABLE WHAT I NEED IS THE # OF EACH DUPLICATE AS SEEN 

//ARRAY *NEWARRAY INIT WITH OBJECTS ,ONEMESS, MULTIPLEMESS,
//ARRAY CUSTOM # OF EACH MULTIPLE MESSAGE TO SHOW IN TBLVIEW???

 //THEN I CAN CONTINUE WITH THE BELOW METHOD TO RELOD THE TABLEVIEW
//[directMessages addObject:NEWARRAY];
//[self.mainTableView reloadData];


return [[duplicateIDs copy] autorelease];

}

这是日志......这种情况发生了很多次,但我只是记录它以查看它是否正常工作。任何帮助将不胜感激

Counted = <NSCountedSet: 0x6d8cb80> (Nkapoor124 [33], iphonetechtips [3], jima6636 [4], pro1995 [2], iphone266 [2], eenofonn [1], iphoneMaster323 [3], FreeAppl3 [2], Bababooey13 [7], _CL1CK_ [5], _ToyVan_ [4], Nucleiosis [26], dracossaint [6], AlexInHouston [1], FlybriTn [7], iOSGraphix [1], deckedsg [5], 19est80 [3], JHackstar [2], barsoverbeats [7], anonomys209 [5], 87whitestallion [1], bAdGigabit [55], Sgt_Impacto [3], Nazaroth [2], K_Nitsua [1], MannyIphone [6], iphoneman76 [3])

1 个答案:

答案 0 :(得分:28)

在您的澄清中,您询问了每份副本的数量(数量?)。你可以得到下面的计数。如果你正在寻找每个重复的次数(而不是每个的总数),那么每个的计数为1。如果它是计数 - 1是0,则没有重复。

我认为您正在寻找NSCountedSet countForObject方法。

以下示例......

    NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil];
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];

    for (id item in set)
    {
        NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
    }

将输出:

2011-09-30 01:00:50.665 Craplet[7854:707] Name=Jane, Count=1
2011-09-30 01:00:50.679 Craplet[7854:707] Name=John, Count=2