当我在dropDown中选择时如何添加项目,我可以显示类别列表并可以选择它,但如何将相关项目链接到父类并保存。
类别表:-
import CoreData
extension Category {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Category> {
return NSFetchRequest<Category>(entityName: "Category")
}
@NSManaged public var categoryColor: String?
@NSManaged public var categoryName: String?
@NSManaged public var items: NSSet?
}
物品类别:-
extension ItemForm {
@nonobjc public class func fetchRequest() -> NSFetchRequest<ItemForm> {
return NSFetchRequest<ItemForm>(entityName: "ItemForm")
}
@NSManaged public var itemName: String?
@NSManaged public var price: Int64
@NSManaged public var parent: Category?
}
在这里,当我选择类别时,我需要显示。
在此itemViewController中,如果我保存,则表示当我选择该类别按钮时应保存有关其父类的信息,而下拉菜单则应保存在该父类别中。
ItemViewController:-
var buttonIndex : Int?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dropdownBtn.setTitle("\(arrCategory[indexPath.row].categoryName!)", for: .normal)
animated(toogle: false)
buttonIndex = indexPath.row
}
@IBAction func saveAction(_ sender: UIButton) {
let selectedIndexInPopup = categoryType.title(for: .normal)
itemSaveData()
self.navigationController?.popViewController(animated: true)
}
func itemSaveData(){
guard let itemName = itemName.text else { return }
guard let price = Int64(itemPrice.text!) else { return }
guard let parentCategorty = buttonIndex else { return }
let itemList = [
"itemName": itemName,
"price": price
"parentCategory": parentCategorty
] as [String : Any]
if isUpdate{
editItemData(itemList: itemList, index: indexRow)
isUpdate = false
}else{
saveItemData(itemList: itemList)
}
}
func saveItemData(itemList: [String:Any]){
let item = NSEntityDescription.insertNewObject(forEntityName: "ItemForm", into: context) as! ItemForm
item.itemName = itemList["itemName"] as! String
item.price = itemList["price"] as! Int64
item.parent = (itemList["parentCategory"] as? Category)
do{
try context.save()
print("Save Successfully")
}catch let err{
print("college save error :- \(err.localizedDescription)")
}
}
}
答案 0 :(得分:0)
我认为您在问如何向现有的CategoryClass添加新的ItemClass对象。
首先,您应该使用primary keys来标识您的商品和类别
class CategoryClass: Object {
@objc dynamic var category_id = NSUUID().uuidString
@objc dynamic var categoryName : String = ""
let items = List<ItemClass>()
override static func primaryKey() -> String? {
return "category_id"
}
}
与ItemClass相同
class ItemClass: Object {
@objc dynamic var item_id = NSUUID().uuidString
@objc dynamic var itemName : String = ""
@objc dynamic var price : Int64 = 0
var parentCategory = LinkingObjects(fromType: CategoryClass.self, property: "items")
}
您似乎正在尝试保留类别的类变量
var categories = CategoryVaraible.categoryArray //not sure what this is
但不要那样做,
var categoryResults = Results<CategoryClass>? = nil
,并将categoryResults用作tableView的数据源。
当用户选择一个类别然后添加项目数据时,要保存它,您将基于在类别弹出窗口中选择的行获得该类别,然后创建一个项目,最后将其添加到写事务中:
func saveItemToCategory() {
let category = self.categoryResults[selectedIndexInPopup]
let item = ItemClass()
item.itemName = some_item_name
item.price = some_item_price
try realm.write {
category.items.append(item)
}
}