第一步:添加新的Mode Vision
操作步骤:选择xxxx.xcdatamodeld文件,点击Editor--> add model vision,创建新的xxxx.xcdatamodeld,在新的xxxx.xcdatamodeld文件中添加你要添加字段。
第二步:选择当前xxxx.xcdatamodeld
操作步骤:在属性栏找到model vision 选择新的xxxx.xcdatamodeld
第三步:appDelegate.swift中修改配置如下
原来配置:
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
修改配置:
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
// Report any error we got.
//
var sqlPathRoot:NSString = url.absoluteString!
var shmurl:NSURL = NSURL(string: sqlPathRoot.stringByAppendingString("-shm"))!
var walurl:NSURL = NSURL(string: sqlPathRoot.stringByAppendingString("-wal"))!
NSFileManager.defaultManager().removeItemAtURL(url, error: nil)
NSFileManager.defaultManager().removeItemAtURL(shmurl, error: nil)
NSFileManager.defaultManager().removeItemAtURL(walurl, error: nil)
//在创建一次
var persistentStore = coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error)
if persistentStore == nil
{
print("版本更替失败了")
}
else
{
print("版本更替成功")
}
}