如何从swift中的核心数据中获取日期类型属性?

时间:2015-03-27 11:07:46

标签: objective-c swift

在我的Notes应用程序中,我想为UIDatePicker Coredata中的{{1}}添加任务到期日期或目标日期的选项,但现在我如何获取它?我想取它并将其存放在标签中.....请给我答案

1 个答案:

答案 0 :(得分:1)

这是你可以从CoreData中读出值的方法:

// context是您的NSManagedObjectContext

var fetchRequest = NSFetchRequest(entityName: "YourEntity")
myData = context!.executeFetchRequest(fetchRequest, error: nil) as [YourCoreDataClass]

所以在myData中有一个对象数组 - 如果只有一个对象存储库,请查看:

if(myData.count > 0) {
   myLabel.text = myData.last!.property //(the property here for the last entry)
}

您也可以使用谓词来过滤对象(例如特定值)

let myPredicate = NSPredicate(format: "id = %@", anyValue)
fetchRequest.predicate = myPredicate

在这里阅读更多内容: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/index.html