0

I have problem with my code. I want compare two dates, but it write erorr.

fatal error unexpectedly found nil while unwrapping an optional value

It write date3 is nil, but it isn't.

let sweetObject = Task(snapshot: sweet as! FIRDataSnapshot)
                    let dateString3 = sweetObject.dueDate //Is June 11, 2017
                    let dateFormatter3 = DateFormatter()
                    dateFormatter3.dateFormat = "MMM dd, yyyy"
                    let date3 = dateFormatter3.date(from: dateString3!)

                    let dateString = "Jan 28, 2017"
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "MMM dd, yyyy"
                    let date = dateFormatter.date(from: dateString)

                    if date3! < date!{
                        print("Due date is earlier than today!")
                    }

If I add this condition, it write "ok" to console.

if date3 != nil {
 print("ok")
  }

What I do bad and what is the problem and how solve it?

user7355869
  • 53
  • 1
  • 10
  • You are force-unwrapping the value of `dateString3!`, and Firebase is returning `nil` for that value. – JAL Jan 11 '17 at 22:29
  • Think of the `!` operator as the "crash if nil" operator. That's essentially what it is. You should pretend it doesn't exist in code that you write until you get more experience. – Duncan C Jan 12 '17 at 00:28

0 Answers0