Run*_*oop 1 cocoa cocoa-touch core-data objective-c
我有一个具有日期属性的Core Data实体.我想编写一个谓词来提取特定月份内的所有日期,例如7月,无论年份如何.怎么能实现这一目标?谢谢
您可以在您的实体上创建一个新方法,我们将调用该方法monthOfDate.此方法仅使用[self dateAttribute]并NSDateComponents提取日期的月份.
然后你可以编写一个谓词:
//assuming 1-based month indexing
NSPredicate * julyEntities = [NSPredicate predicateWithFormat:@"monthOfDate = 7"];
Run Code Online (Sandbox Code Playgroud)
这里的技巧是意识到谓词的左键路径将导致方法调用.因此,如果将左键路径设置为"monthOfDate",它将最终调用您的monthOfDate方法并使用方法的返回值作为谓词中的比较值.整齐!
| 归档时间: |
|
| 查看次数: |
1141 次 |
| 最近记录: |