-1

I have a force unwrapping issue in swift and I've been fixing everything. So Ive forced unwrapped but I get this error "Initializer for conditional binding must have optional type, not 'Date'. Here is the code.

if let createdat = (object?.object(forKey: "createdAt") as? String){
        if let pastDate = Date(timeIntervalSinceNow: TimeInterval(createdat)!)//Here is where I get the error{

        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
john
  • 507
  • 1
  • 5
  • 15

2 Answers2

0

Since the Date initializer you're using doesn't return an optional (i.e. it's a non-failable initializer), you can't (and it would make no sense to) use it in a optional context (such as the if-let pattern).

Removing the if-let from your code fixes the problem:

if let createdat = (object?.object(forKey: "createdAt") as? String){
    let pastDate = Date(timeIntervalSinceNow: TimeInterval(createdat)!)
}
paulvs
  • 11,963
  • 3
  • 41
  • 66
0

Date() doesn't return an optional, it returns a date. That means you can't use

if let

You can use

let
Mozahler
  • 4,958
  • 6
  • 36
  • 56