从自定义对象数组中提取唯一值

发布于 2024-12-07 07:27:12 字数 2444 浏览 0 评论 0原文

我有一个自定义对象的数组列表。每个对象都包含一个我需要提取的值,claimNO,但不是唯一值,这意味着 5 个对象可能具有相同的 ClaimNO。

我需要做的是创建一个只有唯一的 ClaimNO 的数组。我需要在选择器中显示此内容,并且不能有任何重复的声明NO。

我的对象:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2024-12-14 07:27:12

需要注意的是,对于 objectForKey,检查 nil 以确定是否缺少该键。

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

我认为从设计角度来说,您正在做的事情会起作用,但概括您的索赔类别以包括任何给定索赔编号的所有索赔。保留您的字典,索赔编号键将访问附加到唯一索赔编号的所有给定索赔。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文