2017-10-14 91 views
-3

我試圖從NSMutableDictionary採取價值我不能把它,因爲它的多維如何取值從的NSMutableDictionary

{ 
    "seleted_days" =  (
     TUE, 
     WED 
    ); 
    timings =  (
       (
     ), 
       (
     ), 
       (
         { 
       "close_time" = "05:00 AM"; 
       "open_time" = "02:00 AM"; 
       timelist = "02:00 AM , 03:00 AM , 04:00 AM , 05:00 AM"; 
       timestat = "1,0,0,1"; 
      } 
     ), 
       (
         { 
       "close_time" = "05:00 AM"; 
       "open_time" = "02:00 AM"; 
       timelist = "02:00 AM , 03:00 AM , 04:00 AM , 05:00 AM"; 
       timestat = "1,0,0,1"; 
      } 
     ), 
       (
     ), 
       (
     ), 
       (
     ) 
    ); 
} 

需要從這個NSMutableDictionary

得到開啓時間和關閉時間

我試圖代碼

let array:NSArray = myretrievedic.value(forKey: "timings") as! NSArray 
      for i in 0...array.count 
      { 

      let subArray:NSArray = array.object(at: i) as! NSArray 
      if let dict:NSDictionary = subArray.object(at: 0) as! NSDictionary 
      { 

      let close_time:String = dict.value(forKey: "close_time") as! String 
      let open_time:String = dict.value(forKey: "open_time") as! String 


      print(open_time) 
      print(close_time) 
      } 

當空談到它崩潰,即使我把要是讓statements.i不明白爲什麼,如果我們不聲明working

+2

請出示你的代碼試圖JSON字符串,我想你應該避免使用SWIFT'NSMutableDictionary',而不是使用SWIFT本地字典類型 – 3stud1ant3

回答

-1
let dictionary:NSMutableDictionary = NSMutableDictionary() 

    let array:NSArray = dictionary .value(forKey: "timings") as! NSArray 


    for i in 0...array.count 
    { 
     let subArray:NSArray = array.object(at: 2) as! NSArray 

     if subArray.count > 0 { 

      let dict:NSMutableDictionary = subArray.object(at: 0) as! NSMutableDictionary 

      let close_time:String = dict.value(forKey: "close_time") as! String 
      let open_time:String = dict.value(forKey: "open_time") as! String 
     } 
    } 
+0

我需要得到所有,開放時間和關閉時間 –

1

timings的值是一個數組數組。

這是一個本地Swift解決方案,僅使用Swift本機集合類型。它包含用於反序列化JSON的行。 dataData utf-8編碼

do {  
    if let root = try JSONSerialization.jsonObject(with: data) as? [String:Any], 
     let timings = root["timings"] as? [[Any]] { 
     for timing in timings where !timing.isEmpty { 
      if let times = timing.first as? [String:String] { 
       let openTime = times["open_time"]! 
       let closeTime = times["close_time"]! 
       print(openTime, closeTime) 
      } 
     } 
    } 
} catch { 
    print(error) 
} 
相關問題