简体   繁体   English

从目标C中的数组获取唯一对象集的最佳方法是什么

[英]What is the best way to get unique set of objects from an array in objective C

I have an array with list of model objects 我有一个带有模型对象列表的数组

model object has properties Name, Age, gender, I have date as follows 模型对象具有属性名称,年龄,性别,我的日期如下

user* user1.name = "Bob", 
user1.age = "10",
user1.gender = "M",

user* user2.name = "Bob", 
user2.age = "11",
user2.gender = "M",

user* user3.name = "Woz", 
user3.age = "15",
user3.gender = "M",

user* user4.name = "Woz", 
user4.age = "16",
user4.gender = "M",

Now I need to get the unique name and lesser age as result. 现在,我需要获得唯一的名称并降低年龄。

user* user1.name = "Bob", 
user1.age = "10",
user1.gender = "M",

user* user3.name = "Woz", 
user3.age = "15",
user3.gender = "M",

what would be the efficient way to derive the above result. 得出上述结果的有效方法是什么? Thanks In advance Any Hint or help would appreciated. 在此先感谢任何提示或帮助将不胜感激。

You can simply iterate the array looking for the youngest users, storing each one in a dictionary keyed by name. 您可以简单地迭代数组以寻找最年轻的用户,将每个用户存储在以名称为关键字的字典中。 Once you have iterated the array simply get all of the dictionary values as an array; 迭代数组后,只需将所有字典值作为数组即可;

-(NSArray*)getYoungestUniqueUsersFromArray:(NSArray*)users {

    NSMutableDictionary* youngestUsersDict = [NSMutableDictionary new];

    for (User* user in users) {
        User* currentUser = youngestUsersDict[user.name];
        if (currentUser == nil || user.age < currentUser.age) {
            youngestUsersDict[user.name]=user;
        }
    }

    NSArray* youngestUsers = [youngestUsersDict allValues];

    return youngestUsers;
}
@interface User : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSString *gender;
@end

@implementation User
+ (instancetype)create:(NSString *)name gender:(NSString *)gender age:(int)age {
    User *user = [[User alloc] init];
    user.name = name;
    user.gender = gender;
    user.age = age;
    return user;
}

- (BOOL)isEqual:(id)object {
    if ([object isKindOfClass:[User class]]) {
        return [[(User *)object name] isEqualToString:self.name];
    }
    return NO;
}

- (NSUInteger)hash {
    return self.name.hash;
}
@end



- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *users = @[[User create:@"Bob" gender:@"M" age:11],
                       [User create:@"Bob" gender:@"M" age:9],
                       [User create:@"Bob" gender:@"M" age:10],
                       [User create:@"Woz" gender:@"M" age:25],
                       [User create:@"Woz" gender:@"F" age:36],
                       [User create:@"Woz" gender:@"F" age:23]];

    users = [users sortedArrayWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        User *a = obj1;
        User *b = obj2;

        NSComparisonResult nameResult = [a.name compare:b.name];

        if (nameResult == NSOrderedSame) {
            return a.age > b.age;
        }

        return nameResult;
    }];

    NSSet *unique = [NSSet setWithArray:users];

    for (User *user in unique) {
        NSLog(@"%@: %zd - %@", user.name, user.age, user.gender);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在目标C中填充字符串数组的最佳方法 - Best way to populate an array of Strings in objective C 在Objective C中设置for循环的并发执行的最佳方法是什么? - What's the best way to set up concurrent execution of for loops in Objective C? 保存自定义Objective-c对象的层次结构以实现持久性的最佳方法 - Best way to save a hierarchy of custom Objective-c objects for persistence 获取链接并消除所有混乱,仅关注内容-在Objective-C中做到这一点的最佳方法是什么? - Get a link and removing all the clutter and only focusing on the content - what's the best way to do this in Objective-C? 使用Objective-C,在没有API的情况下,登录服务并从结果页面中抓取内容的最佳方法是什么? - With Objective-C, what is the best way to log in to a service and scrape content from the resulting page without an API? 这是设置目标c数组彼此相等的最佳方法吗? - Is this the best way to set objective c arrays equal to oneanother? 在Objective-C中实现此滚动UI的最佳方法是什么? - What is the best way to implement this scrolling UI in Objective-C? 在Objective-C中以特定间隔增加NSDateComponents的最佳方法是什么? - What is the best way to increment NSDateComponents in specific intervals in Objective-C? 在Objective C中跨类共享方法的最佳方法是什么? - What is the best way to share methods across classes in Objective C? 硬编码这些数据的最佳方法是什么? Objective-C的 - What is the best way of hardcoding this data. Objective-C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM