美文网首页
Swift2.1学习CoreData(1)

Swift2.1学习CoreData(1)

作者: 伽蓝香 | 来源:发表于2015-11-24 14:57 被阅读20次

时间格式化闭包写法


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)

      本文链接:https://www.haomeiwen.com/subject/mtbrhttx.html