swift - returning inside 'if let' and else -


i have following function. understand should provide return case datevalue nil. returning nil isn't allowed compiler. twist im thought?

func datefromisostring(datestring: string) -> nsdate {      var dateformatter = nsdateformatter()     dateformatter.dateformat = "yyyymmdd"      let datevalue: nsdate? = dateformatter.datefromstring(datestring)      if let datevalue = datevalue {         return datevalue     }else{         //return what?     } } 

you're not allowed return nil because you're not returning optional - therefore change return type nsdate?.

because, if datefromstring fails parse string returns nil anyway, don't need bother if let statement. instead, write:

return dateformatter.datefromstring(datestring) 

Comments