Core Data使用之一(Swift): 获取

时间:2022-11-03 15:39:01

//获取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")

}

}

}