NSDictionary:MutableCopy之后的比较

时间:2013-10-14 18:27:05

标签: objective-c nsmutabledictionary nsmutablecopying

我有以下属性:

@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的mutableCopy是否将其副本更改为不更改任何内容的位置,您将其与源进行比较并且它不相同?我无法弄清楚我做错了什么!

1 个答案:

答案 0 :(得分:1)

如果两个词典各自具有相同数量的条目,则它们具有相同的内容;对于给定的键,每个词典中的相应值对象满足isEqual:test。这就是equaltodictionary的工作方式。所以问题看起来你的内容没有持有相同数量的条目。请重新检查你的mutabledictinary对象。而且我注意到的另一件事是你的一个条件,你在比较相同的数据类,尽管有一个myclass。