根据该Array Objective C中的重复值获取Array列表

时间:2018-03-12 09:26:19

标签: objective-c nsarray

我坚持以下问题。我收到了以下回复。

(
 {
     CarrId = 102;
     CarrName = "Fast and Furious";
     CarrOptions =         (
                               {
                                   Id = 8;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 9;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "M-30";
                               },
                               {
                                   Id = 10;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 11;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "M-30";
                               },
                               {
                                   Id = 12;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "L-30";
                               },
                               {
                                   Id = 13;
                                   Img = "738.jpg";
                                   PImg = "749.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 14;
                                   Img = "738.jpg";
                                   PImg = "749.jpg";
                                   PQ = "M-30";
                               }
                               );
     Status = 1;
 }
 )

根据该阵列目标C中的重复值获取阵列列表。我需要根据PImg获取阵列。例如,如果PImg = 412.jpg。我需要下面的数组

                               {
                                   Id = 8;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 9;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "M-30";
                               }

我只是从响应中获取重复项

NSMutableDictionary * thisRow = [resultArray objectAtIndex:0];
NSMutableArray  *pdctDtls = [thisRow objectForKey:@"CarrOptions"];

NSOrderedSet *orderedSet = [[NSOrderedSet orderedSetWithArray:pdctDtls] valueForKey:@"PImg"];
NSMutableArray *newarray = [orderedSet mutableCopy];

在newarray中我得到重复的值。但如果在新阵列中,如果412.jpg获得与412.jpg相关的数组。 TIA

2 个答案:

答案 0 :(得分:0)

试试这个。如果您通过611.jpg,则会获得3个对象。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", @"611.jpg"]; // AS PER YOUR REQUIREMENT
NSArray *array =[pdctDtls filteredArrayUsingPredicate:predicate];

答案 1 :(得分:0)

您可以像这样使用NSPredicate:

NSArray *pdctDtls = thisRow[@"CarrOptions"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PImg = %@", @"412.jpg"];
NSArray *resultsWithSamePimg = [pdctDtls filteredArrayUsingPredicate:predicate];
if (resultsWithSamePimg.count > 1) {
    // we have some duplicates
    // ...
}

但我宁愿使用普通的旧for循环,这可能更容易调试和理解。