Binary operator '<' cannot be applied to two 'Date?' operands

Hey guys. I have the error: "Binary operator '<' cannot be applied to two 'Date?' operands" popping up after changing this:

class TaskItem: Object, Identifiable {
    @Persisted(primaryKey: true) var id: ObjectId
    @Persisted var taskTitle: String
    @Persisted var taskNotes: String
    @Persisted var taskDate: Date = Date()
    @Persisted var isFinished: Bool
    @Persisted var taskTime: Date = Date()
    @Persisted var isNotificationsEnabled: Bool
    @Persisted var taskColor: String
    
    static func &lt; (lhs: TaskItem, rhs: TaskItem) -> Bool {
        if lhs.taskDate == rhs.taskDate {
            return lhs.taskTitle &lt; rhs.taskTitle
        } else {
            return lhs.taskDate &lt; rhs.taskDate
        }
    }
}

to:

class TaskItem: Object, Identifiable {
    @Persisted(primaryKey: true) var id: ObjectId
    @Persisted var taskTitle: String
    @Persisted var taskNotes: String
    @Persisted var taskDate: Date? = nil
    @Persisted var isFinished: Bool
    @Persisted var taskTime: Date? = nil
    @Persisted var isNotificationsEnabled: Bool
    @Persisted var taskColor: String
    
    static func &lt; (lhs: TaskItem, rhs: TaskItem) -> Bool {
        if lhs.taskDate == rhs.taskDate {
            return lhs.taskTitle &lt; rhs.taskTitle
        } else {
            return lhs.taskDate &lt; rhs.taskDate
        }
    }
}

Please help as fast as possible, since there is almost no more time left for the swift student challenge.

Also, what would be the nil version for Date variables?

An optional may be nil, and then you cannot compare them. You would need to use taskDate! to unwrap the optional, but then if it is nil, that would crash the app. Another option is to use taskDate? but you'd need to provide a default value to use in case the date is nil, using ??.

Another option would be to do:

if let first = lhs.taskDate, let second = res.taskDate  {
   return first.taskDate &lt; second.taskDate
} else {
   return false
}

Or something along that way.

BTW, do you really need taskDate and taskTime, since a Date holds both the date and time information?

Are you using a Package as Realm ?

Yes

I can't find out how to show a date picker that lets the user select the date and time.

You need to be careful here. Our pickers traditionally work in terms of a Date value but that doesn’t necessarily mean that you should use a Date value in your model. For example, if you store a user’s birthday, you shouldn’t store it as a Date but rather as a DateComponents. That’s because a birthday isn’t an absolute fixed point in time, but rather a date whose time varies based on the context.

Given this mismatch, you ofter have to convert between date components and a Date. Do that using Calendar. On the way in, test the the hour component to midday so that you don’t hit this issue touched on in Parsing Dates Without Times.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Binary operator '&lt;' cannot be applied to two 'Date?' operands
 
 
Q