倍数插入在核心数据
问题描述:
我有很多的信息来重新填充我的数据库,这就是我该怎么办:倍数插入在核心数据
let notaryFeeGrid = NSEntityDescription.insertNewObject(forEntityName: "NotaryFeeGrid", into: context) as! NotaryFeeGrid
notaryFeeGrid.id = 0
notaryFeeGrid.notaryLoansAmount = 1000
notaryFeeGrid.notaryFeeAmount = 510
context.insert(notaryFeeGrid)
let notaryFeeGrid2 = NSEntityDescription.insertNewObject(forEntityName: "NotaryFeeGrid", into: context) as! NotaryFeeGrid
notaryFeeGrid2.id = 0
notaryFeeGrid2.notaryLoansAmount = 3000
notaryFeeGrid2.notaryFeeAmount = 530
context.insert(notaryFeeGrid2)
let notaryFeeGrid3 = NSEntityDescription.insertNewObject(forEntityName: "NotaryFeeGrid", into: context) as! NotaryFeeGrid
notaryFeeGrid3.id = 0
notaryFeeGrid3.notaryLoansAmount = 5000
notaryFeeGrid3.notaryFeeAmount = 570
context.insert(notaryFeeGrid3)
它的完整代码的一小部分,我怎么能减少呢?
答
试试这个......创建公证项目的阵列..
更新:
func addNotary(_ context : NSManagedObjectContext,_ notaryArray: Array) -> Bool {
for index in 0...notaryArray.count - 1
{
let notary : Dictionary = notaryArray[index] as! Dictionary
let notaryFeeGrid = NSEntityDescription.insertNewObject(forEntityName: "NotaryFeeGrid", into: context) as! NotaryFeeGrid
notaryFeeGrid.id = notary["id"]
notaryFeeGrid.notaryLoansAmount = notary["notaryLoansAmount"]
notaryFeeGrid.notaryFeeAmount = notary["notaryFeeAmount"]
}
var error: NSError?
let success = context.save(&error)
if !success {
print("Save failed")
}
}
让您所有的重复代码的功能。然后对于每个“插入”,请调用你的函数。 – AgRizzo
@AgRizzo有可能使用类似数组的东西做得更好? – Ben