NSDictionary:可变复制后的比较



我有以下属性:

@property (retain, nonatomic) NSMutableDictionary * firstStartTimeObject;
@property (retain, nonatomic) NSMutableDictionary * firstLocationNameObject;
@property (retain, nonatomic) NSMutableDictionary * firstLocationAddressObject;
@property (retain, nonatomic) NSMutableDictionary * secondStartTimeObject;
@property (retain, nonatomic) NSMutableDictionary * secondLocationNameObject;
@property (retain, nonatomic) NSMutableDictionary * secondLocationAddressObject;

这就是我复制字典的方式:

-(DataClass *)copyObjects
{
    DataClass *newClass = [[DataClass alloc]init];
    newClass.firstStartTimeObject = [firstStartTimeObject mutableCopy];
    newClass.firstLocationAddressObject = [firstLocationAddressObject mutableCopy];
    newClass.firstLocationNameObject = [firstLocationNameObject mutableCopy];
    newClass.secondStartTimeObject = [secondStartTimeObject mutableCopy];
    newClass.secondLocationNameObject = [secondLocationNameObject mutableCopy];
    newClass.secondLocationAddressObject = [secondLocationAddressObject mutableCopy];
    return newClass;
}

在另一个类中,我比较了它们

if([myClass.firstStartTimeObject isEqualToDictionary:dataClass.firstStartTimeObject])
{
    [dataClass.firstStartTimeObject setValue:kCellBackGroundColor forKey:kBackGround];
}
if([myClass.firstLocationNameObject  isEqualToDictionary:dataClass.firstLocationNameObject])
{
    [dataClass.firstLocationNameObject setValue:kCellBackGroundColor forKey:kBackGround];
}
if([dataClass.firstLocationAddressObject  isEqualToDictionary:dataClass.firstLocationAddressObject])
{
    [dataClass.firstLocationAddressObject setValue:kCellBackGroundColor forKey:kBackGround];
}
if([myClass.secondStartTimeObject isEqualToDictionary:dataClass.secondStartTimeObject])
{
    [dataClass.secondStartTimeObject setValue:kCellBackGroundColor forKey:kBackGround];
}
if([myClass.secondLocationNameObject  isEqualToDictionary:dataClass.secondLocationNameObject])
{
    [dataClass.secondLocationNameObject setValue:kCellBackGroundColor forKey:kBackGround];
}
if([myClass.secondLocationAddressObject  isEqualToDictionary:dataClass.secondLocationAddressObject])
{
    [dataClass.secondLocationAddressObject setValue:kCellBackGroundColor forKey:kBackGround];
}

我有断点设置。 比较字典中的键/值是相同的,但编译器似乎以不同的方式看待它们,因为它在大括号内并命中断点的条件永远不会成立。

我通过 NSLog 验证了键/值,它们是相同的。 我什至尝试了- (id)mutableCopyWithZone:(NSZone *)zone协议并得到了相同的行为。

NSMutableDicitonary的可变副本是否将其副本更改为不更改其任何内容的位置,您将它与源进行比较并且它不一样? 我不知道我做错了什么!

如果两个字典各自拥有相同数量的条目,并且对于给定键,每个字典中的相应值对象满足 isEqual: 测试,则它们具有相同的内容。这就是 equaltodictionary 的工作方式,因此问题看起来您的内容没有包含相同数量的条目。请重新检查可变字典对象。我注意到的另一件事是,尽管有一个 myclass,但您正在比较相同的数据类,您的 if 条件是。

最新更新