2013-07-24 116 views
0

我正在處理一個非常基本的問題,我有兩個字典鍵我需要比較,但是當我這樣做並不給我正確的答案,不知何故我無法創建邏輯。NSDictionary鍵比較失敗

所以我有一個plist數組的字典和json數組來自服務器 我想比較他們如果事件存在於plist做某件事情,如果不做另一件事;

問題:
根據我的邏輯如果在plist中兩個事件是等於其它兩個事件從服務器正在添加如果邏輯應該每每個字典從服務器來進行打印,所以在這種情況下的總的2倍,但在我的代碼中它被打印4次。基本上每次都滿足條件。

的NSLog:

evreka find the id 
evreka find the id 
evreka find the id 
evreka find the id 

SOURCE: 在plist中;

createList (
     { 
     "end_date" = "2013-07-24"; 
     ends = "13:07:00"; 
     "event_id" = 173; 
     "event_name" = Static; 
     location = Asdad; 
     "root_folder" = "[email protected]"; 
     "start_date" = "2013-07-24"; 
     starts = "13:07:00"; 
     "user_id" = 13; 
    }, 
     { 
     "end_date" = "2013-07-25"; 
     ends = "13:08:00"; 
     "event_id" = 174; 
     "event_name" = "Event Delete"; 
     location = Asdsad; 
     "root_folder" = "[email protected]"; 
     "start_date" = "2013-07-25"; 
     starts = "13:08:00"; 
     "user_id" = 13; 
    } 
) 

這從正在添加服務器

responseobj (
     { 
     "end_date" = "2013-07-24"; 
     ends = "13:07:00"; 
     "event_id" = 173; 
     "event_name" = Static; 
     location = Asdad; 
     "root_folder" = "[email protected]"; 
     "start_date" = "2013-07-24"; 
     starts = "13:07:00"; 
     "user_id" = 13; 
    }, 
     { 
     "end_date" = "2013-07-25"; 
     ends = "13:08:00"; 
     "event_id" = 174; 
     "event_name" = "Event Delete"; 
     location = Asdsad; 
     "root_folder" = "[email protected]"; 
     "start_date" = "2013-07-25"; 
     starts = "13:08:00"; 
     "user_id" = 13; 
    } 
) 

CODE:

比較的代碼;

if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){ 
     createList= [NSMutableArray arrayWithContentsOfFile:dataPath]; 
     NSLog(@"createList %@",createList); 
     NSLog(@"responseobj %@",responseObj); 


     //compare plist and response object 
     for (int i=0; i<[responseObj count]; i++) { 
      NSDictionary *dataDict = [responseObj objectAtIndex:i]; 

      NSString *event_name =[dataDict objectForKey:@"event_name"]; 
      //NSString *event_id =[dataDict objectForKey:@"event_id"]; 

      BOOL eventExistInPlist=NO; 


      for (int j=0; j<[createList count]; j++) { 
       NSDictionary *createDict = [createList objectAtIndex:i]; 

       //NSString *create_id =[createDict objectForKey:@"event_id"]; 
       NSNumber *create_id =[createDict objectForKey:@"event_id"]; 

       if ([[dataDict objectForKey:@"event_id"] isEqual:[createDict objectForKey:@"event_id"]]) { 
       // if (create_id==event_id) { 
        NSLog(@"evreka find the id"); 

我曾嘗試: 我試圖把它們比爲字符串(isEqualtoString),數字(==)和isEqual:方法的方法,他們都失敗了。

我在做什麼錯?

+0

首先,使事情通過提取的'值更簡單和更混亂[dataDict objectForKey:@「事項標識」]'內循環外面並將其保存在局部變量中。 (我看到你做了這件事,爲什麼呢?)爲什麼沒有你提取的值的NSLogs - 他們可能會很快發現你的問題? –

回答

3

NSDictionary *createDict = [createList objectAtIndex:i]; 
                 ^----- HERE 

您使用了錯誤的指標,應該是j而不是i爲內循環。

+0

OMG!,謝謝 –

+0

@MordFustang:不客氣! –

1

馬丁R的答案是你的問題。我會改變你的邏輯,儘管包括Objective-C快速枚舉而不是++循環的標準。我還分離出的第二個for循環到BOOL方法更容易閱讀

for (NSDictionary *dataDict in responseObj) { 
    NSString *event_name =[dataDict objectForKey:@"event_name"]; 
    if ([self matchFoundForEventId:event_name]) { 
     NSLog(@"Match found"); 
    } 
} 

-(BOOL)matchFoundForEventId:(NSString *)eventId { 
    for (NSDictionary *dataDict in createList) { 
     NSString *createEvent = [dataDict objectForKey:@"event_id"]; 
     if ([eventId isEqualToString:createEvent]) { 
      return YES; 
     } 
    } 
    return NO; 
}