Convert NSDate to Date
You can use a function or just an extension:
let nsDate = NSDate()
let date = Date(timeIntervalSinceReferenceDate: nsDate.timeIntervalSinceReferenceDate)
Just make implicit casting like:
let nsdate = NSDate()
let date = nsdate as Date
result.date
is an optional NSDate
, so you can bridge it
to an optional Date
:
result.date as Date?
Then use optional binding to safely unwrap it. In your case that could be
guard let date = result.date as Date? else {
// date is nil, ignore this entry:
continue
}
You might also want to replace
let commnt = String(describing: result.commnt)
with
guard let commnt = result.commnt else {
// commnt is nil, ignore this entry:
continue
}
otherwise you'll get comment strings like Optional(My comment)
.
(Rule of thumb: String(describing: ...)
is almost never what you
want, even if the compiler suggests it to make the code compile.)