ios - Swift2: String to date, format it and back to string. Found nil -


i have date string: sun, 07 feb 2016 21:16:21 +0000

and need in: dd.mm.yyyy

following method throws fatal error: nil

if parsedelement == "date" {     if currentarticle.date.isempty {          let dateformatter = nsdateformatter()          dateformatter.dateformat = "eee, dd mmm yyyy hh:mm:ss z"         let tempdate = dateformatter.datefromstring(str)         dateformatter.dateformat = "dd.mm.yyyy"         let converteddate = dateformatter.stringfromdate(tempdate!)          currentarticle.date = converteddate      } } 

debug:

str string "sun, 07 feb 2016 21:16:21 +0000"

tempdate nsdate? 2016-02-07 21:16:21 utc 0xe41bc67eba500000​

converteddate string "07.02.2016"

looks great, moment release "currentarticle.date = converteddate"​ says converteddate​ = nil

ideas?

ps: currentarticle.date == isempty

edit: if loop runs 5 times. if give currentarticle.date = str (only date string), debugger says:

1. str string "sun, 07 feb 2016 21:16:21 +0000"

2. str ""

3. str string "thu, 04 feb 2016 21:18:34 +0000"

4. str ""

5. str string "thu, 04 feb 2016 18:57:14 +0000"

you're getting fatal error:

unexpectedly found nil while unwrapping optional value -> tempdate

i assume you're getting error in line:

let converteddate = dateformatter.stringfromdate(tempdate!) 

this because you're force unwrapping optional equal nil. should use optional binding unwrap it, i.e.:

if let tempdate = tempdate {     let converteddate = dateformatter.stringfromdate(tempdate)     currentarticle.date = converteddate } 

now question of why date formatter not returning value str in instance data. can add else above optional binding code block display str in instance narrow down problem.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -