我想构建一个可散列值用于我的字典键。它应该由一个包含两个字符串和一个 NSDate 的结构组成。我不确定我hashValue
在下面正确构建了我的吸气剂:
// MARK: comparison function for conforming to Equatable protocol
func ==(lhs: ReminderNotificationValue, rhs: ReminderNotificationValue) -> Bool {
return lhs.hashValue == rhs.hashValue
}
struct ReminderNotificationValue : Hashable {
var notifiedReminderName: String
var notifiedCalendarTitle: String
var notifiedReminderDueDate: NSDate
var hashValue : Int {
get {
return notifiedReminderName.hashValue &+ notifiedCalendarTitle.hashValue &+ notifiedReminderDueDate.hashValue
}
}
init(notifiedReminderName: String, notifiedCalendarTitle: String, notifiedReminderDueDate: NSDate) {
self.notifiedReminderName = notifiedReminderName
self.notifiedCalendarTitle = notifiedCalendarTitle
self.notifiedReminderDueDate = notifiedReminderDueDate
}
}
var notifications: [ReminderNotificationValue : String] = [ : ]
let val1 = ReminderNotificationValue(notifiedReminderName: "name1", notifiedCalendarTitle: "title1", notifiedReminderDueDate: NSDate())
let val2 = ReminderNotificationValue(notifiedReminderName: "name1", notifiedCalendarTitle: "title1", notifiedReminderDueDate: NSDate())
notifications[val1] = "bla1"
notifications[val2] = "bla2"
notifications[val2] // returns "bla2".
notifications[val1] // returns "bla1". But I'd like the dictionary to overwrite the value for this to "bla2" since val1 and val2 should be of equal value.