Core Data Predicate Filter By Today's Date

后端 未结 3 861
耶瑟儿~
耶瑟儿~ 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 10:00

    In swift4, Lawrence413's can be simplify a bit:

    //Get today's beginning & end
    let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
    let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)
    

    It get rid of the component part, makes the code have better readability.

提交回复
热议问题