我想以編程方式迅速創建一個新的屬性到已經存在的實體。
這是我的代碼:斯威夫特崩潰時新CoreData屬性到現有實體
let model = NSManagedObjectModel()
var properties = Array<NSAttributeDescription>()
let remoteURLAttribute = NSAttributeDescription()
remoteURLAttribute.name = "\(foundArray[indexPath.row]) id"
remoteURLAttribute.attributeType = .stringAttributeType
remoteURLAttribute.isOptional = false
remoteURLAttribute.isIndexed = true
properties.append(remoteURLAttribute)
entity?.properties = properties
model.entities = [entity!]
應用程序總是與此錯誤崩潰:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't modify an immutable model.'
我該如何解決這個問題?
你是對的,但我想添加一個新的屬性到一個已經存在的實體。我怎樣才能做到這一點? –
通常的方法是[創建新版本的模型文件並進行模型遷移](https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/Introduction.html )。可以在代碼中修改模型,但**非常不尋常,因此即使這樣,您仍然需要進行模型遷移。 –
那麼最好的選擇是:創建一個新的模型還是找到另一種方式而不創建新的屬性? –