Code is not formatted in comments, making it hard ro read.
1. let model = Calendar(context: context)
2. print(model.dateSaved)
3. if model.dateSaved == dateFromFirstView {
4. print("dates are identical ",model.dateSaved," ",dateFromFirstView)
5. } else {
6. print("dates are not identical ",model.dateSaved," ",dateFromFirstView)
7. }
8.
9. @IBAction func datePickerChanged(_ sender: Any) {
10. let dateFormatter = DateFormatter()
11. dateFormatter.dateFormat = "dd-MM-YYYY"
12. dateFromFirstView = dateFormatter.string(from: datePicker.date)
13. }
14.
15. @IBAction func datePickerChanged(_ sender: Any) {
16. let dateFormatter = DateFormatter()
17. dateFormatter.dateFormat = "dd-MM-YYYY"
18. strDate = dateFormatter.string(from: datePicker.date)
19. print(strDate)
20. }
So model is a variable for the Entity,
dateSaved is an attribute of the entity and it is a string (as I use a date formatter that converts the date from a date picker into a string).
dateFromFirstView is also a string.
The thing is that I added a print but the if else statement only gets triggered once so if I change the date on the datePicker the if else statement doesn't get triggered.
Then I store the strDate as dateSaved in Core Data. I want the if else statement to get triggered whenever I select a new date on the date picker but it only works one time.
Some questions about this code:
datePickerChanged is defined twice. Is it just a copy and paste error ?
In which is the code from lines 1 to 7 ? If it is in viewDidload, that's normal to be called only once.
What you could do is
keep model as a global in your class
keep dateFromFirstView as a global in your class
create a func to test:
func compareDates() {
if model.dateSaved == dateFromFirstView {
print("dates are identical ", model.dateSaved," ", dateFromFirstView)
} else {
print("dates are not identical ", model.dateSaved," ", dateFromFirstView)
}
}
replace lines 3 to 7 with compareDates()
call it in datePickerChanged
@IBAction func datePickerChanged(_ sender: Any) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-YYYY"
dateFromFirstView = dateFormatter.string(from: datePicker.date)
compareDates()
}