【问题标题】:Make sets from array, which contains array of custom objects从数组中创建集合,其中包含自定义对象的数组
【发布时间】:2017-04-20 10:09:54
【问题描述】:
NSMutableArray *arrayDetails = [[NSMutableArray alloc] init];
CustomClass *country1       = [[CustomClass alloc] init];
country1.strCountryName     =  @"USA";
country1.code               =  @"12";
[arrayDetails addObject:country1];

CustomClass *country2       = [[CustomClass alloc] init];
country2.strCountryName     =  @"India";
country2.code               =  @"234";
[arrayDetails addObject:country2];

CustomClass *country4       = [[CustomClass alloc] init];
country4.strCountryName     =  @"UK";
country4.code               =   @"34";
[arrayDetails addObject:country4];

CustomClass *country5       = [[CustomClass alloc] init];
country5.strCountryName     =  @"USA";
country5.code               =  @"12";
[arrayDetails addObject:country5];

CustomClass *country6       = [[CustomClass alloc] init];
country6.strCountryName     =  @"India";
country6.code               =  @"12";
[arrayDetails addObject:country6];

CustomClass *country7       = [[CustomClass alloc] init];
country7.strCountryName     =  @"India";
country7.code               =  @"12";
[arrayDetails addObject:country7];

CustomClass *country8       = [[CustomClass alloc] init];
country8.strCountryName     =  @"USA";
country8.code               =  @"12";
[arrayDetails addObject:country8];

CustomClass *country3       = [[CustomClass alloc] init];
country3.strCountryName     =  @"UK";
country3.code               =  @"12";
[arrayDetails addObject:country3];


CustomClass *country77       = [[CustomClass alloc] init];
country77.strCountryName     =  @"PAK";
country77.code               =  @"12";
[arrayDetails addObject:country3];

我想制作基于国家/地区名称的单独集合,所有自定义类对象,其中包含一个数组中的美国、数组中的印度和一个数组中的英国,甚至是单个对象 PAK。 数据来自服务器,基于我要分组的 strContry 名称。

【问题讨论】:

  • 这里有什么自定义类...?
  • 只是一个模型类
  • 你必须使用 NSPredicate 来区分自定义类中的字段匹配后。我仍然没有代码。但这应该是这样的

标签: ios objective-c nsarray


【解决方案1】:

将所有对象添加到“arraydetails”后,尝试下面的代码来实现。

NSArray *countryNames = [[NSOrderedSet orderedSetWithArray:[arrayDetails valueForKey:@"strCountryName"]] array];
    for (int i =0; i < [countryNames count]; i++) {
        NSMutableArray *countryArray = [[NSMutableArray alloc] init];
        for (CustomClass *country in arrayDetails) {
            if ([country.strCountryName isEqualToString:[countryNames objectAtIndex:i]]) {
                [countryArray addObject:country];
            }
        }
        NSLog(@"Country array[%d] is %@", i, countryArray);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2022-01-18
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多