Remove duplicates from array comparing the properties of its objects

前端 未结 6 1308
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 03:50

Suppose I have a class Event, and it has 2 properties: action (NSString) and date (NSDate).

And suppose I have an array of Event objects. The problem is that \"date\" pr

6条回答
  •  无人共我
    2021-02-04 04:21

    Wouldn't this work with kvc. I suppose the following solution could work in your case;

    Event *event1 = [[Event alloc] init];
    event1.name = @"Event1";
    event1.date = [NSDate distantFuture];
    Event *event2 = [[Event alloc] init];
    event2.name = @"Event2";
    event2.date = [NSDate distantPast];
    Event *event3 = [[Event alloc] init];
    event3.name = @"Event1";
    event3.date = [NSDate distantPast];
    NSArray *array = @[event1, event2, event3];
    
    NSArray *filteredEvents =  [array valueForKeyPath:@"@distinctUnionOfObjects.name"];
    

提交回复
热议问题