问题描述
基本上我有一个名为TimeLoc的实体,它有三个属性:时间,纬度和经度。它们的类型分别是Date,Double和Double。
Basically I have an entity called TimeLoc that has three attributes: time, latitude and longitude. And their types are Date, Double and Double respectively. I got stuck when I was trying to make a request with filter on attribute time.
这是我的代码:
...
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var context: NSManagedObjectContext = self.managedObjectContext!
var request = NSFetchRequest(entityName: "TimeLoc")
var endTime = NSDate()
var startTime = NSDate(timeIntervalSinceNow: -65)
let predicate = NSPredicate(format: "time>= \(startTime) AND time<= \(endTime)")
request.predicate = predicate
var results: NSArray = context.executeFetchRequest(request, error: nil)!
...
我收到以下错误:
'无法解析格式字符串time> = 2015-07-14 03:24:03 +0000 AND time< = 2015-07-14 03:25:08 +0000'
And I got the following error:'Unable to parse the format string "time>= 2015-07-14 03:24:03 +0000 AND time<= 2015-07-14 03:25:08 +0000"'
我应该如何解决这个问题?我有google,但所有的解决方案,我发现在Objective-C。如果有人可以在Swift回答我,这将是巨大的。
How should I fix this? I have googled a bit but all the solutions I found were in Objective-C. It would be great if someone can answer me in Swift.
编辑:V-Xtreme建议,这个问题的答案可以找到Glauco Neves的答案
As V-Xtreme suggested, an answer to this problem can be find in Glauco Neves's answer to this question
推荐答案
我想你以NSDate格式向谓词发送参数:
I guess you are sending parameters to the predicate in NSDate format :
let predicate = NSPredicate(format: "time>= \(startTime) AND time<= \(endTime)")
b $ b
这里你的startTime和endTime的类型是NSDate。尝试以String格式转换它并将其传递给谓词。它应该工作。
Here your startTime and endTime is of type NSDate . Try to convert it in String format and pass it to the predicate . It should work .
参考:
这篇关于CoreData使用NSPredicate(swift)有条件地在NSDate上提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!