首页 > 代码库 > Core Data使用之一(Swift): 获取
Core Data使用之一(Swift): 获取
//获取entity中的数据
func fetchFiles() {
//第一步,获取总代理
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
//第二步,获取对象管理上下文
let managedContext = appDelegate.managedObjectContext
//第三步,获取entity的请求对象
let fetchRequest = NSFetchRequest(entityName: "Article")
//第四步,获取结果集
let fetchResults = managedContext?.executeFetchRequest(fetchRequest, error: nil) as [NSManagedObject]?
//第五步, 赋值
if (fetchResults != nil) {
filesTable = fetchResults!
for result in filesTable {
let name = result.valueForKey("name") as String?
let content = result.valueForKey("content") as String?
NSLog("\(name), \(content)\n")
}
}
}
Core Data使用之一(Swift): 获取