CoreData的使用步骤
let context=NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
//模型文件
let model=NSManagedObjectModel.mergedModelFromBundles(nil)
//持久化调度器
let store=NSPersistentStoreCoordinator.init(managedObjectModel: model!)
//设置沙盒路径
let doc=NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true).last
let sqlitePath=doc!.stringByAppendingString("/company.sqlite")
//数据存储的类型和数据库存储路径
try! store.addPersistentStoreWithType(NSsqliteStoreType,configuration: nil,URL: NSURL.fileURLWithPath(sqlitePath),options: nil)
context.persistentStoreCoordinator=store
//保存对象到数据库
let sb=NSEntityDescription.insertNewObjectForEntityForName("Enployee",inManagedObjectContext: context)as! Enployee
sb.age=13
sb.name="nievvsmei"
try! context.save()
//读取信息
let request=NSFetchRequest.init(entityName:"Enployee")
let emps=try!context.executeFetchRequest(request)
for sb2 in emps{
print(sb2.age,sb2.name)
}