pulling unique values from an array of custom objects

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

i have an array list of custom objects. each object contains a value i need to pull out, claimNO, but is not a unique value, meaning say 5 objects may have the same claimNO.

what i need to do is make an array that only has unique claimNO's. i need to display this in a picker and can't have any repeating claimNO.

my object:

@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 

to me, this should work:

            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");             } 

but my value for "[ClaimCenterClaimNOList objectForKey:claimCenterClaim.claimID]" is always null untill after the if statement.

if i have a claimID value, can't i just check if that key value in the dictionary already exists, and if it doesn't, add it?

i would like to avoid needing to iterate thru the ClaimCenterClaimNOList dictionary (creates a loop in a loop). but i know the key, can't i just see if that key already exists in the dictionary?

EDIT: incorrect logic

my claimID values are unique, so i was checking my dictionary if a claimID was already added to the dictionary. since claimID is unique, it never found a match. i switched the search around and is now working. here's the correct code:

             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");                 }             } 

回答1:

Couple points, for the objectForKey, check for nil to determine the absence of the key.

Also, you could just drop the array of claims into an NSSet that seems to closer to the desired behavior. But I am unsure, if you want to just access one or all of the claims for any given claim number.

I think design wise what you are doing will work, but generalize your claim class to include all the claims for any given claim number. Keep you dictionary and the claim number key will access all of the given claims attached to a unique claim number.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!