简体   繁体   中英

check for nil and extract data from dictionary in swift

If the user provides a date value I store it in a dictionary:

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

If the user does not provide a date value I store it in a dictionary like this:

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

I tried to retrieve the value from the dictionary inside cellForRowAt function like this:

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)

This is my date to string function

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)
}

I get error on the guard statement:

Type 'String' is not optional value can never be nil

How can I check if date is not nil and get the date converted and if date is nil skip it and put in a statement as “None” instead of Date?

The error in guard statement is because the method getDateString: not returns an optional value. you are checking the return value of that method which will never be nil as per definition. You need to check date value from dict as it is optional.

try like,

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
}

Try this:

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 
}

Just add optional to return value of your method

**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)
    }

If you want to use unwrapping right do it like this:

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)

and you function should look like this:

    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
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM