【问题标题】:How do I use realm on swift to store array of objects?如何在 swift 上使用领域来存储对象数组?
【发布时间】:2020-10-24 16:36:13
【问题描述】:

在之前的一个问题中,我询问了如何将类的实例存储在本地数据库中,有人建议我使用领域作为存储方法。我已经设置了我的领域并完成了研究,但是,在我的课堂上,我有一个对象数组,当我尝试将对象数组设置为“@objc 动态”以便将其存储到领域文件中时,它不允许这样做,抛出错误“属性不能被标记@objc,因为它的类型不能在Objective-C中表示”。我真的不明白这是什么意思,或者在 Objective-C 中可以表示什么”。

//
//  MealPlan.swift
//  IA
//
//  Created by Ian Dong on 8/25/20.
//  Copyright © 2020 Ian Dong. All rights reserved.
//

import Foundation
import RealmSwift
class MealPlan: Object {
    
    @objc dynamic var ingerdientList: [IngerdientCost] = [];
    var k: Int = 0;
    var serving : Int
    @objc dynamic var cost : [IngerdientCost] = [];
    var name : String;
    
    init ( size: Int, name: String){
        self.serving = size;
        self.name = name;
    }
    func addIngerdient (newIngerdient: IngerdientCost){
        ingerdientList.insert(newIngerdient, at: k);
        k = k+1;
    }
    func removeINgerdient(theIngerdient: IngerdientCost)
    {
        var c: Int = 0;
        while theIngerdient.amount != ingerdientList[c].amount && c <= k
        {
            c=c+1;
        }
        ingerdientList.remove(at: c);
        k=k-1;
    }
    func getCost() -> Array<IngerdientCost>
    {
        var c: Int = 0;
        while c <= ingerdientList.count
        {
            cost[c].ingerdientName = ingerdientList[c].ingerdientName;
            cost[c].amount = ingerdientList[c].amount * Double(serving);
            c = c+1;
        }
        return cost;
    }
    func setServing( serv: Int)
    {
        self.serving = serv;
    }
}



//
//  Ingerdient cost .swift
//  IA
//
//  Created by Ian Dong on 8/25/20.
//  Copyright © 2020 Ian Dong. All rights reserved.
//

import Foundation
import RealmSwift

class IngerdientCost: Object{
    
    init(Name: Ingerdient, Amount: Double )
    {
        self.amount = Amount;
        self.ingerdientName = Name;
    }
    
    var ingerdientName: Ingerdient;
    var amount: Double;
    
}


【问题讨论】:

  • IngerdientCost 还必须符合Object
  • 另外,你应该使用List而不是数组:var ingerdientList: List&lt;IngerdientCost&gt;()
  • 如何让 IngerdientCost 符合 Object?
  • 列表和数组有什么区别?
  • 您的领域对象丢失self.init()。请参阅Models - 对象子类的自定义初始化程序部分。此外,这对于领域列表对象@objc dynamic var ingerdientList: [IngerdientCost] = []; 是不正确的。需要是let ingredientList = List&lt;IngredientCost&gt;()。花几分钟时间阅读 Realm 中的列表Getting Started Guide

标签: objective-c swift database realm


【解决方案1】:

在 Realm 中,您需要使用 List 而不是自定义类的数组(例如 IngerdientCost)。对于List,不使用@objc dynamic,将needs声明为let

class MealPlan: Object {
    
    let ingerdientList = List<IngerdientCost>() /// no @objc dynamic ! but you need to use let
    var k: Int = 0
    var serving : Int
    let cost = List<IngerdientCost>() /// also no @objc dynamic !
    var name : String

    ...

}

那么IngerdientCost需要符合Object

class IngerdientCost: Object {
    @objc dynamic var ingerdientName: Ingerdient? /// to-one relationships must be optional → https://realm.io/docs/swift/latest/#relationships
    @objc dynamic var amount = Double(0)
}

Array 的大部分属性和方法都在List 中可用,比如.count,你可以循环它(for object in ingerdientList {})。

附带说明,没有理由在每行末尾使用;

【讨论】:

  • 但在我的代码中,我使用了数组属性,列表仍然有效吗?
  • for...in 之类的大多数东西都可以使用。让我检查一下。
  • 好吧,例如“.count”和数组引用,例如“array[c]”
  • 这是declarationListListArray 都符合 MutableCollection 允许您执行 array[c]List 也符合 RealmCollection(Realm 的自定义类型之一),它支持 .count
  • 我已经添加了上面 IngerdientCost 的代码,我想我已经完成了你告诉我的事情,但它仍然抛出同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多