2011-09-28 51 views
0

我有一个自定义对象的数组列表。每个对象都包含一个我需要提取的值,即claimNO,但不是唯一的值,也就是说5个对象可能具有相同的claimNO。从自定义对象数组中提取唯一值

我需要做的是使一个数组只有唯一的声明号。我需要在选取器中显示它,并且不能有任何重复的声明号。

我的对象:

@interface ClaimCenterClaim : NSObject 
{ 
    NSNumber *claimID; 
    NSString *claimNO; 
    NSNumber *coid; 
    NSString *eventDates; 
} 

@property (nonatomic, retain) NSNumber *claimID; 
@property (nonatomic, retain) NSString *claimNO; 
@property (nonatomic, retain) NSNumber *coid; 
@property (nonatomic, retain) NSString *eventDates; 

@end 

对我来说,这应该工作:

  NSMutableDictionary *ClaimCenterClaimNOList = [[NSMutableDictionary alloc] init]; 

      int count01 = [sortedClaimList count]; 
      for (int i = 0; i < count01; i++) 
      { 
       claimCenterClaim = [sortedClaimList objectAtIndex:i]; 

       if ([ClaimCenterClaimNOList objectForKey:claimCenterClaim.claimID] != claimCenterClaim.claimNO) 
       { 
        NSLog(@"entered the bloody loop"); 
        [ClaimCenterClaimNOList setObject:claimCenterClaim.claimNO forKey:claimCenterClaim.claimID]; 
       } 
       else 
        NSLog(@"did not add value"); 
      } 

但我对价值 “[ClaimCenterClaimNOList objectForKey:claimCenterClaim.claimID]” if语句之后总是空,直到。

如果我有claimID值,我不能检查字典中的键值是否已经存在,如果不存在,请添加它?

我想避免需要迭代通过ClaimCenterClaimNOList字典(在循环中创建一个循环)。但我知道钥匙,我不知道钥匙是否已经存在于字典中了吗?

编辑:不正确的逻辑

我ClaimID的值是唯一的,所以我检查我的字典里,如果一个ClaimID的已添加到字典中。由于claimID是唯一的,它从来没有找到匹配。我切换搜索周围,现在正在工作。这里是正确的代码:

   int count01 = [sortedClaimList count]; 
      for (int i = 0; i < count01; i++) 
      {      
       claimCenterClaim = [sortedClaimList objectAtIndex:i]; 

       NSLog(@"lets see before: claimCenterClaim.claimiD: %@ the object: %@",claimCenterClaim.claimID, [ClaimCenterClaimNOList objectForKey:claimCenterClaim.claimID]); 

       if ([ClaimCenterClaimNOList objectForKey:claimCenterClaim.claimNO] == nil) 
       { 
        NSLog(@"not in the dictionary"); 
        [ClaimCenterClaimNOList setObject:claimCenterClaim.claimID forKey:claimCenterClaim.claimNO]; 
       } 
       else 
       { 
        NSLog(@"it works, it is in the dictionary"); 
       } 
      } 

回答

0

几个百分点,对于objectForKey,检查nil确定缺席的关键。

此外,您可以将索赔数组放入NSSet,似乎更接近所需的行为。但我不确定,如果您只想访问任何给定索赔编号的一项或全部索赔。

我认为明智的设计可以发挥作用,但是请概括您的理赔类以包含任何给定理赔号的所有理赔。保留字典和索赔号码密钥将访问附加到唯一索赔号码的所有索赔。

+0

感谢您搜索零的提示,它有帮助。另外,我从来没有使用过NSSet,也不确定它的用途,但我会研究它,谢谢! – Padin215