iOS比较两个对象数组

时间:2014-11-20 00:08:03

标签: ios arrays nsarray compare mkmapview

我试图比较两个对象数组,看它们是否包含相同的数据。其中一个数组是地图视图的注释,另一个是自定义对象数组。它们是否处于相同的顺序并不重要,只是我想确保它们包含相同的对象。我想根据对象的一个​​属性来比较它们。如果一个数组比另一个数组多或少,它也必须失败。以下是我迄今所取得的成就。

在下面的示例中,它不应该失败,因为它们包含相同的对象。感谢。

代码:

NSSet *set1 = [NSSet setWithArray:[[FCDataController sharedInstance] alarmData]];
NSSet *set2 = [NSSet setWithArray:self.mapView.annotations];

if ([set1 isEqualToSet:set2]) {
    NSLog(@"both are same");
}
else{
    NSLog(@"both are differnt");
}

NSLog(@"Alarm = %@", set1);
NSLog(@"Map Data = %@", set2);

结果:

2014-11-19 16:03:04.662 FireCom[298:8787] both are differnt
2014-11-19 16:03:04.663 FireCom[298:8787] Alarm = {(
    <FCCall: 0x141aec0b0> Allergic C1 (45.501204, -122.916716),
    <FCCall: 0x141b4eb60> Breathing Prob. (45.401885, -122.284218),
    <FCCall: 0x141af6be0> Trauma (45.309056, -122.745360),
    <FCCall: 0x141b53030> Sick Person/unko (45.343586, -122.792549),
    <FCCall: 0x13de78a60> Chest Pain/heart (45.499593, -122.881558),
    <FCCall: 0x141b4fc00> Odor Investigate (45.290905, -122.331344),
    <FCCall: 0x141af68b0> Sick Person/unko (45.366520, -122.620110),
    <FCCall: 0x141af6d10> Invalid Assist (45.393609, -122.806652)
)}
2014-11-19 16:03:04.665 FireCom[298:8787] Map Data = {(
    <FCCall: 0x13de7b3f0> Sick Person/unko (45.366520, -122.620110),
    <FCCall: 0x13de63e80> Odor Investigate (45.290905, -122.331344),
    <FCCall: 0x13de7b780> Sick Person/unko (45.343586, -122.792549),
    <FCCall: 0x13de7b520> Trauma (45.309056, -122.745360),
    <FCCall: 0x13de63fb0> Chest Pain/heart (45.499593, -122.881558),
    <FCCall: 0x13de7b2c0> Allergic C1 (45.501204, -122.916716),
    <FCCall: 0x13de7b650> Invalid Assist (45.393609, -122.806652),
    <FCCall: 0x13de63d50> Breathing Prob. (45.401885, -122.284218)
)}

编辑:

- (BOOL)isEqualToCall:(FCCall *)call
{
    // If we have no call
    if (!call) {

        // Then fail
        return NO;
    }

    // Return the check
    return (!self.incident && !call.incident) || [self.incident isEqualToString:call.incident];
}

- (BOOL)isEqual:(id)object
{
    // If they have the same memory addresses
    if (self == object) {

        // They are the same
        return YES;
    }

    // If they are not the same kind of class
    if (![object isKindOfClass:[FCCall class]]) {

        // They are not the same
        return NO;
    }

    // Return check
    return [self isEqualToCall:(FCCall *)object];
}

- (NSUInteger)hash
{
    // Returh the hash
    return [self.incident hash];
}

1 个答案:

答案 0 :(得分:0)

我用两个字典做了一个例子,但是对象中的属性与谓词中字典中的键相同。

我希望你能找到:

// I have created two array the dict in order to test.
NSArray *alarmArray = @[@{@"name":@"Allergic",@"clock":@2},@{@"name":@"Person",@"clock":@2},@{@"name":@"Invalid Assist",@"clock":@2}];

NSArray *annotationsArraY = @[@{@"name":@"Allergic",@"lat":@2},@{@"name":@"Sick Person",@"lat":@2},@{@"name":@"Invalid Assist",@"lat":@2},@{@"name":@"OtroNom",@"lat":@2},@{@"name":@"last",@"lat":@2}];


// The filered Array:
NSArray *filteredArray;
if ([annotationsArraY count] > [alarmArray count])
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in %@.name",alarmArray];
    filteredArray = [annotationsArraY filteredArrayUsingPredicate:predicate];
}
else
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in %@.name",annotationsArraY];
    filteredArray = [alarmArray filteredArrayUsingPredicate:predicate];

}



NSLog(@"Exit array is: %@",[filteredArray description]);

}