Core Data Predicate Filter By Today's Date

后端 未结 3 862
耶瑟儿~
耶瑟儿~ 2020-12-15 09:07

I had issues with this and I haven\'t found a proper answer on SO so I\'ll leave a small tutorial here.

The goal is to filter fetched objects by today\'s date.

3条回答
  •  醉酒成梦
    2020-12-15 09:52

    Might be helpful to add to Lawrence413's answer that to filter a list of records with an attribute holding today's date, you could use :

     let fromPredicate = NSPredicate(format: "datetime >= %@", dateFrom as NSDate)
    let toPredicate = NSPredicate(format: "datetime < %@",  dateToUnwrapped as NSDate)

    ...Where "datetime is the name of the attribute"

提交回复
热议问题