pod ‘MagicalRecord’安装MagicalRecord
/*************************以下为教程****************************/
AppDelegate.swift
import MagicalRecord
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
MagicalRecord.setupCoreDataStack()//只加这句话初始化
return true
}
ViewController.swift
import MagicalRecord
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/*创建*/
let admin = Admin.MR_createEntity()//创建
admin?.token = "1234"//编辑
print("创建")
NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()//保存
/*创建结束*/
/*查询*/
let admins = Admin.MR_findAll()//查询
for a in admins as! [Admin]{
print(a.token)
a.token = "liuman"//编辑
//a.MR_deleteEntity()//删除
}
print("条件查询token值为liuman的")
let liuman = Admin.MR_findFirstByAttribute("token", withValue: "liuman")
print(liuman!.token)
/*查询结束*/
//Admin.MR_truncateAll()//删除所有
NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()//创建编辑删除之后都要保存,原save方法,被取代了。
//MagicalRecord.cleanUp()//app退出前操作,关闭coredata
}