2013-10-14 27 views
0

我有以下特性:的NSDictionary:比較後MutableCopy

@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是否將其副本更改爲其中沒有更改任何內容的位置,將它與源對比並且它不相同?我無法弄清楚我做錯了什麼!

+0

if([dataClass.firstLocationAddressObject isEqualToDictionary:dataClass.firstLocationAddressObject])爲什麼你這樣比較這種情況? –

+0

你是什麼意思?我需要比較字典以查看是否有任何鍵值已更改。 – user1107173

+0

不,我的意思是,你是比較你的兩個數據類的條件,儘管我的一類 –

回答

1

如果兩個字典中的每個字典都擁有相同數量的條目,並且對於給定的鍵,每個字典中對應的值對象滿足isEqual:test,則兩個字典具有相同的內容。這就是equaltodictionary工作的原因。因此,問題看起來像您的內容不具有相同數量的條目。請重新檢查您的mutabledictinary對象。還有一件事我注意到,如果你在比較相同的dataclass(儘管有一個myclass)的情況下,你的一個。