Swift2.1学习CoreData(1)

时间格式化闭包写法


lazy var dateFormatter: NSDateFormatter = {

let formatter = NSDateFormatter()

formatter.dateStyle = .ShortStyle

formatter.timeStyle = .MediumStyle

return formatter

}()

数据库存储操作流程


//存储前先生成数据库对象实例

let dogEntity = NSEntityDescription.entityForName("Dog", inManagedObjectContext: managedContext)

let currentDog = Dog(entity: dogEntity!,

insertIntoManagedObjectContext: managedContext)

currentDog.name = dogName

do {

try managedContext.save()

} catch let error as NSError {

print("Error: \(error) " + "description \(error.localizedDescription)")

}

删除数据库数据


let walkToRemove = currentDog.walks![indexPath.row] as! Walk

managedContext.deleteObject(walkToRemove)

do {

try managedContext.save()

} catch let error as NSError { print("Could not save: \(error)")

}

你可能感兴趣的:(Swift2.1学习CoreData(1))