如何使用领域(Swift)将项目添加到其各自的父类别中>> [

问题描述 投票:0回答:1
[当我在dropDown中选择时如何添加项目,我可以显示类别列表并可以选择它,但如何将相关项目链接到父类并保存它。

类别表:-

import Foundation import RealmSwift class CategoryForm : Object { @objc dynamic var categoryName : String = "" let items = List<Items>() }

物品类别:-

import RealmSwift class Items: Object { @objc dynamic var itemName : String = "" @objc dynamic var price : Int64 = 0 var parentCategory = LinkingObjects(fromType: CategoryForm.self, property: "items") }

enter image description here

在这里,当我选择需要显示的类别时。

在这个itemViewController中,如果我保存的意思是当我选择该类别按钮时,它应该相对于其父类保存,因为下拉时,它应该保存在该父类别中。

ItemViewController:-

var itemArray = ItemVaraible.itemArry var categories = CategoryVaraible.categoryArray func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { categoryType.setTitle("\(categories![indexPath.row].categoryName)", for: .normal) animated(toogle: false) } @IBAction func saveAction(_ sender: UIButton) { let newArray = Items() newArray.itemName = itemName.text!//These two title and done are from core data entity let price = Int64(itemPrice.text!) newArray.price = price! if saveFlag == true { self.saveItems(category: newArray) } else { self.updateItems(category: newArray)//update } self.navigationController?.popViewController(animated: true) } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { categoryType.setTitle("\(categories![indexPath.row].categoryName)", for: .normal) animated(toogle: false) } func saveItems(category: Items) { do{ try realm.write {//Save/Create realm.add(category) } }catch { print("Error in saving \(error)") } } func updateItems(category: Items) { do{ try realm.write {//Save/Create realm.add(category, update: .modified) } }catch { print("Error in saving \(error)") } } //Read the data after saving like displying again func displayItems() { itemArray = realm.objects(Items.self) } func displayCategories() { categories = realm.objects(CategoryForm.self) tableview.reloadData() }

当我在dropDown中选择时如何添加项目,我可以显示类别列表,也可以选择它,但是如何将相关项目链接到父类并保存。类别表格:-import Foundation ...
ios swift realm parent
1个回答
0
投票
我认为您正在问如何将新的ItemClass对象添加到现有的CategoryClass。
© www.soinside.com 2019 - 2024. All rights reserved.