0

我有 2 个 NSMutableArrays,其中包含 Person 类的一个实例。我需要检查两个数组中是否有任何“名称”值相同的人,并将其合并并询问替换具有相同值“名称”的 Reson 实例。

看起来像:

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


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

然后程序询问将 empl1 中的 Person @"Paul" 替换为 empl2 中的 Person @"Paul" 并将任何新人员从 empl2 添加到 empl2

结果必须是(如果我们替换 Paul):

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

想想这两天,但没有成功。请帮忙 :)

4

2 回答 2

1

您可以在 Person 上实现-isEqual:andhash并将所有对象放在一个 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 中,则只会保留第一个具有相同名称的对象。另一个将被检测为重复并且不存储在集合中。

更多信息:集合编程主题


#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;
}
于 2013-04-19T01:38:27.383 回答
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];

这是文档这是 NSSet 可变版本的文档

于 2013-04-19T10:12:34.363 回答