谓词抛出异常

时间:2015-05-28 21:44:41

标签: ios core-data

我不知道为什么我在这段代码上收到错误:

 //fecha y hora actual


        let date = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
        let hour = components.hour
        let minutes = components.minute
        let year = components.year
        let month = components.month
        let day = components.day
        print (year)
        let predicate = NSPredicate(format: "year_created >= %@", year)
        fetchRequest.predicate = predicate

该行引发异常:

let predicate = NSPredicate(format: "year_created >= %@", year)

其中“year_created”是int16核心数据属性。

欢迎任何帮助。

SCREENSHOT补充道:

enter image description here

1 个答案:

答案 0 :(得分:1)

感谢截图。我的猜测是,它并不完全是NSPredicate的问题。尝试将代码更改为:

let predicate = NSPredicate(format: "year_created >= %ld", year)

NSDateComponent的年份属性是NSInteger,因此您无法在格式中使用%@。