How to compare dates from two view controllers (Core Data and Swift)?

I am new to Swift and I have a problem with comparing dates with Core Data. I have two views: the first one with a date picker and a table, and the second one with a text field and another date picker. I save the date and the text from the text field into Core Data (from the 2nd view). I want to populate the table (in the first view) with the text from the text field (from the second view) when the selected date on the first date picker equals the date from the second date picker (which is saved into Core Data). I would appreciate any help. I thought about this solution but it doesn't work:

    
    let model = eventsItems[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
    if model.dateSaved == dateFromFirstView{
        cell.textLabel!.text = model.event
    }
    return cell
}
Answered by Claude31 in 683453022

it doesn't work

That tells nothing, please explain what you get (or don't get) and what you expected.

Are model.dateSaved and dateFromFirstViewboth Date ? If so, are the dates exactly the same ? You should add some print to log:

    if model.dateSaved == dateFromFirstView {
        print("Dates are the same")
        cell.textLabel!.text = model.event
    } else {
        print(model.dateSaved, "is not the same as", dateFromFirstView)
    }

And tell what you get.

Accepted Answer

it doesn't work

That tells nothing, please explain what you get (or don't get) and what you expected.

Are model.dateSaved and dateFromFirstViewboth Date ? If so, are the dates exactly the same ? You should add some print to log:

    if model.dateSaved == dateFromFirstView {
        print("Dates are the same")
        cell.textLabel!.text = model.event
    } else {
        print(model.dateSaved, "is not the same as", dateFromFirstView)
    }

And tell what you get.

I'll repeat my answer, because the code looks bizarre.

Right, sorry. 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.

let model = Calendar(context: context)  
  print(model.dateSaved)   
 if model.dateSaved == dateFromFirstView{   
   print("dates are identical ",model.dateSaved," ",dateFromFirstView)    }  
  else{      
print("dates are not identical ",model.dateSaved," ",dateFromFirstView)   
} 
 @IBAction func datePickerChanged(_ sender: Any){
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-YYYY"
     
    dateFromFirstView = dateFormatter.string(from: datePicker.date)
   
  }
  @IBAction func datePickerChanged(_ sender: Any){
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-YYYY"
     
    strDate = dateFormatter.string(from: datePicker.date)
    
    print(strDate)
    
  }
   

 And 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.

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()
}
How to compare dates from two view controllers (Core Data and Swift)?
 
 
Q