如何使用Swift 4从数组中分离值

时间:2018-05-16 12:29:59

标签: swift

如何使用Swift 4从数组中分离值。以下是我的数据:

arrWeekly == (
     { 
         date = "2018-04-30";
         units = "g/dL"; 
         value = 12;
     }, 
     { 
         date = "2017-06-27"; 
         units = "g/dL";
         value = "14.5";
     }  
)

我的代码:

if let arrMonthly = dictPeriod["monthly"] as? [Any], arrMonthly.count > 0 
{ 
    self.arrMonth = NSMutableArray(array: arrMonthly)
    print("arrMonthly == ",self.arrMonth)
}else{ 
    self.arrMonth = NSMutableArray()
}

我想分开两个日期&值。

2 个答案:

答案 0 :(得分:1)

    if let arrMonthly = dictPeriod["monthly"] as? [[AnyHasahble:String]], ! arrMonthly.isEmpty { 
    for disc in arrMonthly{
       if let date = disc["date"] as? String{

        }
       if let units = disc["units"] as? String{

        }
       if let value = disc["value"] as? String{

        }
    }
}else{ 

}

答案 1 :(得分:0)

让dictPeriod = YOUR_DICTIONARY

        guard let arrMonthly = dictPeriod["monthly"] as? [[String: Any]], !arrMonthly.isEmpty else { return }

        let dateArr  = arrMonthly.map({ $0["date"] as! String })
        let unitsArr = arrMonthly.map({ $0["units"] as! String })
        let valueArr = arrMonthly.map({ $0["value"] as! String })