检查nil并从swift中的字典中提取数据

时间:2017-08-21 04:26:06

标签: ios swift date dictionary

如果用户提供日期值,我将其存储在字典中:

let dict:Dictionary<String, Any> = ["fromDate":fromDate as Any]

如果用户没有提供日期值,我将其存储在如下字典中:

let dict:Dictionary<String, Any> = ["fromDate": [:] ]

我试图从cellForRowAt函数中的字典中检索值,如下所示:

let dict:Dictionary<String, Any> = allDates[indexPath.row]
guard let fromDate:String = getDateString(createdDate: dict["fromDate"] as! Date) != nil else {
        fromDate:String = "None"
}
cell.fromDateLabel.text = getTimeStringISO8601(createdDate: fromDate)

这是我的字符串函数日期

func getDateString(createdDate:Date) -> String{
    let formatter = DateFormatter()
    formatter.calendar = Calendar(identifier: .iso8601)
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.locale = Locale.current
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter.string(from: createdDate)
}

我在警卫声明中收到错误:

  

Type'String'不是可选值,永远不能为nil

如何检查日期是否为零并获取转换日期,如果日期为零则跳过它并将其作为“无”而不是日期放入声明?

4 个答案:

答案 0 :(得分:1)

guard语句中的错误是因为方法getDateString:不返回可选值。您正在检查该方法的返回值,该值根据定义永远不会为零。您需要从dict检查日期值,因为它是可选的。

试试,

var fromDate = "None" 
if let dateVal = dict["fromDate"] as? Date {
    fromDate = getDateString(createdDate: dateVal)
    cell.fromDateLabel.text = getTimeStringISO8601(createdDate: fromDate)
} else {
    cell.fromDateLabel.text = fromDate
}

答案 1 :(得分:0)

试试这个:

let dict:Dictionary<String, Any> = allDates[indexPath.row]

if let date = dict["fromDate"] as? Date {
    // the date is not nil
    //here you can formate your date
}else{
   //the date is nil, make additional stuff here 
}

答案 2 :(得分:0)

只需添加可选方法的返回值

即可
**func getDateString(createdDate:Date) -> String?**


class UIViewController: UIViewController {

       guard let fromDate:String = getDateString(createdDate: Date())  else {

          return

        }
    }


    func getDateString(createdDate:Date) -> String?{
        let formatter = DateFormatter()
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.locale = Locale.current
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.string(from: createdDate)
    }

答案 3 :(得分:0)

如果您想使用展开权,请执行以下操作:

let dict:Dictionary<String, Any> = allDates[indexPath.row]
guard let fromDate: String = getDateString(createdDate: dict["fromDate"] as? Date) else   {
    fromDate:String = "None"
}
 cell.fromDateLabel.text = getTimeStringISO8601(createdDate: fromDate)

你的功能应该是这样的:

    func getDateString(createdDate:Date?) -> String?{
      if let createdDate = createdDate {
         let formatter = DateFormatter()
         formatter.calendar = Calendar(identifier: .iso8601)
         formatter.locale = Locale(identifier: "en_US_POSIX")
         formatter.locale = Locale.current
         formatter.timeZone = TimeZone(secondsFromGMT: 0)
         formatter.dateFormat = "yyyy-MM-dd"
         return formatter.string(from: createdDate)
      }

      return nil
   }