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

Get the Array list based on the duplicate values in that Array Objective C

我遇到了以下问题。我收到以下回复。

(
 {
     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;
 }
 )

根据数组中的重复值获取数组列表Objective 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

试试这个。如果你通过 611.jpg,你得到 3 个对象。

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

您可以像这样使用 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 循环,这样可能更容易调试和理解。