这里介绍在 Playground 中使用 CoreData 的小技巧。
//: Playground - noun: a place where people can play
import UIKit
import CoreData
// Core Data Stack Setup for In-Memory Store
public func getModelContext(name:String) -> NSManagedObjectContext {
// Replace "Model" with the name of your model
let modelUrl = NSBundle.mainBundle().URLForResource(name, withExtension: "momd")
guard let model = NSManagedObjectModel.init(contentsOfURL: modelUrl!) else { fatalError("not this file") }
let psc = NSPersistentStoreCoordinator(managedObjectModel: model)
try! psc.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil)
let context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
context.persistentStoreCoordinator = psc
return context
}
let context = getModelContext("Model")
// Insert a new Entity
let ent = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context)
ent.setValue("fasf", forKey: "name")
try! context.save()
// Perform a fetch request
let fr = NSFetchRequest(entityName: "Entity")
let result = try! context.executeFetchRequest(fr)
print(result)
结果如图
下载说明
☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,
☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!
☉唯一站长QQ:1004003180 [人格担保-本站注重诚信!]
☉购买建议E-mail:1004003180@qq.com 源码收购 E-mail:1004003180@qq.com
☉本站文件解压密码 【文章内都自带解压密码,每个密码不同!】