如何比较和合并NSMutableArray [英] How to compare and merge NSMutableArray

查看:149
本文介绍了如何比较和合并NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



它看起来像:

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

$ b empl2 = [
人[
name = @Paul,
age = 47,
]
name = @Sean,
age = 30,
]
]

$ b b

然后,在empl2中询问关于替换person @Paul的empl1中的Person @Paul,并将任何新的人从empl2添加到empl2



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

  empl = [
Person [
name = @Paul,
age = 47,
],
person [
name = @John,
age = 36,
],
person [
name = @Sean,
age = 30,
]
]
pre>

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

解决方案

您可以实现 -isEqual: code> hash on Person并将所有对象放在集合中。

  @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:@Paulage:45],
[[Person alloc] initWithName:@Johnage:36]
NSArray * p2Array = @ [[[Person alloc] initWithName:@Paulage:47],
[[Person alloc] initWithName:@Seanage:30]

NSMutableSet * resultSet = [[NSMutableSet alloc] initWithArray:p1Array];
NSMutableSet * duplicateates = [[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);
/ *
这里有一对重复的对象。
,根据你的进一步的要求,stror他们某处
,并在处理它进一步询问用户。
* /
}
}

}
return 0;
}


I have 2 NSMutableArrays that contains an instances of the class Person. I need to check is there any Person with same value of "name" in both of arrays and merge it with ask about replace instances of Reson with same value "name".

It looks like:

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


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

Then program ask about replacement Person @"Paul" in empl1 with Person @"Paul" in empl2 and add any new persons from empl2 to empl2

And result must be (if we replace Paul):

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

Think about this 2 days but without success. Please help :)

解决方案

you could implement -isEqual: and hash on Person and put all objects in a 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

if you now put it into an NSSet or NSOrderedSet just the first object with the same name will be kept. The other will be detected as duplicate and not stored in the set.

For more: 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;
}

这篇关于如何比较和合并NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆