2016-12-16 34 views
0

我是iOS新手,邏輯上有點弱。無法從NSDictionary獲取所有匹配字符串iOS

任何一個可以幫助我在這裏,

我想從NSDictionary中得到所有匹配的數據。

如:

tempDict是的NSDictionary包含此,

tempDict:

errorCode = 00; 
errorMessage = "<null>"; 
pastConsultations =  (

      { 
     date = "16 December 2016"; 
     day = Friday; 
     doctorName = "<null>"; 
     localTime = "07:30 AM"; 
     symptoms = q; 
     today = Yes; 
    }, 
      { 
     date = "16 December 2016"; 
     day = Friday; 
     doctorName = "<null>"; 
     localTime = "07:30 AM"; 
     symptoms = g; 
     today = Yes; 
    }, 
      { 
     date = "13 December 2016"; 
     day = Tuesday; 
     doctorName = "<null>"; 
     localTime = "12:30 PM"; 
     symptoms = fever; 
     today = No; 
    } 
); 
upcomingConsultations =  (
      { 
     date = "16 December 2016"; 
     day = Friday; 
     localTime = "09:30 PM"; 
     symptoms = ""; 
     today = Yes; 
    }, 
      { 
     date = "16 December 2016"; 
     day = Friday; 
     localTime = "09:30 PM"; 
     symptoms = chj; 
     today = Yes; 
    }, 
      { 
     date = "18 December 2016"; 
     day = Sunday; 
     localTime = "12:30 PM"; 
     symptoms = "test an incoming "; 
     today = No; 
    } 
); 
} 

裏面一個pastConsultations關鍵我想所有嵌套數據包含AM

即我的輸出應該是:

{ //Array at 0th index 
      date = "16 December 2016"; 
      day = Friday; 
      doctorName = "<null>"; 
      localTime = "07:30 AM"; 
      symptoms = q; 
      today = Yes; 
     }, 
       { //Array at 1st index 
      date = "16 December 2016"; 
      day = Friday; 
      doctorName = "<null>"; 
      localTime = "07:30 AM"; 
      symptoms = g; 
      today = Yes; 
     } 
} 

這是我試過的代碼,但它不工作,請幫助我犯錯誤?

NSArray *allKeys; 
     for (int i=0; i<[tempDict count]; i++) { 
      allKeys = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] allKeys]; 



      NSString *targetKey = nil; 
      // NSArray *allKeys = [[tempDict valueForKeyPath:@"pastConsultations"] allKeys]; 
      for (int j = 0; j < [allKeys count]; ++j) { 
       NSString *key = [allKeys objectAtIndex:i]; 
       NSString *obj = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] objectForKey:key]; 
       if ([obj rangeOfString:searchText].location != NSNotFound) { // searchedString is what you're looking for 
        targetKey = key; 
        NSLog(@"found match"); 
        break; 
       } 
      } 
     } 
+0

有沒有任何錯誤或者你沒有得到結果? –

+0

沒有得到結果 –

回答

1

試試這個代碼:

假設你已經有了tempDict辭典加載。

NSMutableArray *morningPastArr = [[NSMutableArray alloc] init]; 

NSArray *pastArray = [tempDict objectForKey:@"pastConsultations"]; 
for (int i=0;i<[pastArray count]; i++) { 
    NSDictionary *eachPast = [pastArray objectAtIndex:i]; 
    NSString *time = [eachPast objectForKey:@"localTime"]; 
    if (![time rangeOfString:@"AM"].location == NSNotFound) { 
    [morningPastArr addObject:eachPast]; 
    } 
} 

// finally you will have like what you need in morningPastArr. 

編輯: 如果你要搜索的結果結合起來,例如,結果AM條目,週五只有你可以這樣做:

NSMutableArray *morningAndFridayPastArr = [[NSMutableArray alloc] init]; 

NSArray *pastArray = [tempDict objectForKey:@"pastConsultations"]; 
for (int i=0;i<[pastArray count]; i++) { 
    NSDictionary *eachPast = [pastArray objectAtIndex:i]; 
    NSString *time = [eachPast objectForKey:@"localTime"]; 
    NSString *day = [eachPast objectForKey:@"day"]; 
    if ((![time rangeOfString:@"AM"].location == NSNotFound)&&([day isEqualToString:@"Friday"])) { 
    [morningAndFridayPastArr addObject:eachPast]; 
    } 
} 
+0

爲什麼要使用循環?謂詞比這更有效。 –

+0

是的,我同意。你的代碼更高效。但我不像你那麼專業。我的代碼通常更容易被新手(比如我自己)理解。我很抱歉寫長碼。 – GeneCode

+1

這是工作順利,我是新來的iOS我不知道謂詞。你能說我如何使用謂詞來實現這一點? –

0

您可以使用NSPredicate爲如下:

NSPredicate *predicate; 
NSMutableArray *filteredData=[[NSMutableArray alloc]init]; 

predicate = [NSPredicate predicateWithFormat:@"localTime contains[c] %@",@"AM"]; 
//OR Use below predicate as suggested by Rishi 
predicate = [NSPredicate predicateWithFormat:@"localTime ENDSWITH 'AM'"]; 

filteredData=[[[tempDict valueForKey:@"pastConsultations"] filteredArrayUsingPredicate:predicate]] mutableCopy]; 

希望這會有所幫助。

+0

它返回空''filteredData檢查 –

+0

編輯的答案,還要檢查是否'tempDict'不是'nil' @ Appledeveloper –

+0

我仍然得到'filteredData'作爲零還檢查'tempDict'不是零 –

相關問題