如何在领域中保存为每个项目创建不同行的多个对象



我想要做的是能够在一个表视图中同时将多个对象保存到领域中。我有一个简单的表视图,带有一个简单标签,其中有一个栏按钮,可以将其发送到编辑模式。在编辑模式下,我希望能够将我在编辑模式中选择的所有对象保存到领域中。

我不认为这是保存一个对象数组,因为当我尝试时,它所做的只是在Realm中创建一个空行。

这是我的主视图控制器:

import UIKit
import Realm
import RealmSwift
class ViewController: UIViewController, UITableViewDelegate,

UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var realm: Realm!
let num = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
var testingBool = false
var values: [String] = []
@IBOutlet weak var itemBtn: UIBarButtonItem!
@IBOutlet weak var saveBtn: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
print(Realm.Configuration.defaultConfiguration.fileURL!)
realm = try! Realm()
self.tableView.delegate = self
self.tableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return num.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.txtLbl?.text = "(num[indexPath.row])"
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if testingBool == true {
values.append(num[indexPath.row])
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if testingBool == true {
if let index = values.index(of: num[indexPath.row]) {
values.remove(at: index)
}
}
}
@IBAction func btnPressed(_ sender: Any) {
testingBool = !testingBool
if testingBool == true {
tableView.allowsMultipleSelection = true
tableView.allowsMultipleSelectionDuringEditing = true
itemBtn.title = "cancel"
} else if testingBool == false {
tableView.allowsMultipleSelection = false
tableView.allowsMultipleSelectionDuringEditing = false
itemBtn.title = "item"
}
}
@IBAction func saveBtnPressed(_ sender: Any) {
if testingBool == true {
favorite(label: values)
}
}
func favorite(label: [String]) {
let saveTest = SaveTest()
try? realm!.write {
for string in num {
realm.add(saveTest)
}
}
}
}

这是领域模型:

import Foundation
import RealmSwift
import Realm
class SaveTest: Object {
@objc dynamic var label = ""
convenience init(label: String) {
self.init()
self.label = label
}
}

我想做的是用我选择的多行填充Realm。一个例子是,如果我选择了4行和5行,我想同时将它们保存到领域,但它们在领域中是不同的对象,所以它会创建两行,其中一行是4行,另一行是5行。

我之所以要这样做,也是为了将来能够将多个选定的对象保存到具有多个列的数据库中。如果在这个假设中,我有四列:id、Number、Written和Roman,我希望将每个选定行的数据保存到这个新的领域数据库中。

举个例子,如果我再次选择4和5,它会在领域中创建2个新行,其中一行具有"4"4"和IV,每列中有id,而另一行则具有"5"5"V和每列中的id。

我希望我没有太含糊和困惑。如果我能提供任何帮助,请询问。谢谢


更新:

通过搜索堆栈溢出,并在koropok的帮助下,我找到了如何一次保存多个数组。我发现,当你想一次遍历多个数组时,你可以使用zip((。

这是我拥有的新领域保存功能。顺便说一句,它有自己相应的领域类。

func realmed(label: [String], romanNum: [String], txt: [String]) {
try? realm!.write {
for (stringOne, (stringTwo, stringThree)) in zip(label, zip(romanNum, txt)) {
let realmed = Realmed(label: stringOne, romanNum: stringTwo, txt: stringThree)
realm.add(realmed)
}
}
}

谢谢你的帮助。

您需要在循环中而不是在循环之前初始化SaveTest对象的标签

func favorite(label: [String]) {
try? realm!.write {
for string in label {
let saveTest = SaveTest(label: string)
realm.add(saveTest)
}
}
}

最新更新