如何比较和合并NSMutableArray

时间:2013-04-19 00:40:44

标签: objective-c cocoa nsmutablearray

我有2个NSMutableArrays,它们包含Person类的实例。我需要检查是否有任何人具有相同的值" name"在两个数组中并合并它,询问有关使用相同值"名称"替换Reson的实例。

看起来像:

empl1 = [
    Person [
        name = @"Paul",
        age = 45,
    ],
    Person [
        name = @"John",
        age = 36,
    ]
]


empl2 = [
    Person [
        name = @"Paul",
        age = 47,
    ],
    Person [
        name = @"Sean",
        age = 30,
    ]
]

然后节目询问有关替换人员@" Paul"在empl1与Person @" Paul"在empl2中并添加从empl2到empl2的任何新人

结果必须是(如果我们取代保罗):

empl = [
    Person [
        name = @"Paul",
        age = 47,
    ],
    Person [
        name = @"John",
        age = 36,
    ],
    Person [
        name = @"Sean",
        age = 30,
    ]
]

想想这2天但没有成功。请帮助:)

2 个答案:

答案 0 :(得分:1)

你可以在Person上实现-isEqual:hash并将所有对象放在一个Set中。

@interface Person : NSObject
@property(copy) NSString *name;
@property NSUInteger age;
@end

@implementation Person

-(BOOL)isEqual:(id)otherPerson
{
    if([otherPerson isKindOfClass:[self class]])
        return [self.name isEqual:otherPerson.name];
    return false;
}

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

如果现在将它放入NSSet或NSOrderedSet中,则只保留具有相同名称的第一个对象。另一个将被检测为重复,而不是存储在集合中。

更多信息:Collections Programming Topics


#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(copy) NSString *name;
@property NSUInteger age;

-(id)initWithName:(NSString *)name age:(NSUInteger)age;

@end

@implementation Person


-(id)initWithName:(NSString *)name age:(NSUInteger)age
{
    if(self = [super init])
    {
        _name = name;
        _age = age;
    }
    return self;
}

-(BOOL)isEqual:(id)otherPerson
{
    if([otherPerson isKindOfClass:[self class]]){
        Person *rhsPerson = otherPerson;
        return [self.name isEqualToString:rhsPerson.name];
    }
    return false;
}

-(NSUInteger)hash
{
    return [self.name hash];
}

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@  %lu", self.name, self.age];
}
@end


int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSArray *p1Array = @[[[Person alloc] initWithName:@"Paul" age:45] ,
                             [[Person alloc] initWithName:@"John" age:36]];
        NSArray *p2Array = @[[[Person alloc] initWithName:@"Paul" age:47] ,
                             [[Person alloc] initWithName:@"Sean" age:30]];

        NSMutableSet *resultSet = [[NSMutableSet alloc] initWithArray:p1Array];
        NSMutableSet *duplicates = [[NSMutableSet alloc] initWithArray:p2Array];
        [duplicates intersectSet:resultSet];
        [resultSet addObjectsFromArray:p2Array];

        if ([duplicates count]) {
            for (Person *p in [duplicates allObjects]) {

                NSMutableSet *interSet = [resultSet mutableCopy];
                [interSet intersectSet:[NSSet setWithObject:p]];
                Person *pInSet = [interSet allObjects][0];

                NSLog(@"%@ <-> %@", p, pInSet);
                /*
                 Here you have the pairs of duplicated objects.
                 depending on your further requierements, stror them somewhere 
                 and process it further after asking the user.
                 */
            }
        }

    }
    return 0;
}

答案 1 :(得分:1)

你绝对应该使用NSSet。

以下是一个例子:

NSMutableArray *temp1 = @[@1, @2, @3];
NSMutableArray *temp2 = @[@4, @1, @5];

NSMutableSet *set1 = [NSMutableSet setWithArray:temp1];
NSMutableSet *set2 = [NSMutableSet setWithArray:temp2];

[set1 unionSet:set2];

Here's the documentationAnd here's the documentation of mutable version of NSSet