在swift4中设置和获取数组时发生奇怪的事情

时间:2019-03-06 18:38:55

标签: ios realm swift4

我正在使用存储在RealmDB中的LocalSubject.swift类。

class LocalSubject: Object{

    @objc dynamic var subjectName: String?
    private var modules: [Module] = [Module]()
    @objc dynamic var moduleArrayJSON: String? = nil

    public func addModule(module: Module){
        modules.append(module)
    }

    public func setModules(modules: [Module]){
        self.modules = modules
        print("LocalSubject.swift > setModules() > " + String(describing: modules.count) + " > " + String(describing: self.modules.count))
  }

    public func getModules()-> [Module]{
         print("LocalSubject.swift > getModules() > " + String(describing: modules.count) + " > " + String(describing: self.modules.count))
        return modules.sorted(by: { $0.displayOrder! < $1.displayOrder!})
    }
} 

我通过将“模块”数组转换为json&back将类存储在DB中。 在下面的函数中,使用count = 3正确计算“模块”,然后在LocalSubject的“数学”对象中进行设置。现在,当我尝试从“数学”中获取“模块”时,我总是会得到一个空数组。

   public func createModuleArrayFromJSONInSubjects(){
            let modules = getModuleArrayFromJSON(moduleArrayJSON: (maths?.moduleArrayJSON)!)
            for module in modules{
                print("-> " + module.title!)
            }
            maths?.setModules(modules: modules)
            for module in (maths?.getModules())!{
                print("---> " + module.title!)
            }
  }

我无法弄清楚,似乎很奇怪。 我想念什么吗?

1 个答案:

答案 0 :(得分:1)

Realm对象不支持数组类型,相反,您应该使用Realm类型<!DOCTYPE html> <html> <head> <meta charset='utf-8'> </head> <body> <script> var imgArray = [['http://www.clker.com/cliparts/U/k/6/3/4/M/yellow-rounded-number-0-md.png', 'http://www.clker.com/cliparts/2/5/6/d/1242796868203109724Number_1_in_green_rounded_square.svg.med.png', 'http://www.clker.com/cliparts/k/e/O/E/Y/f/orange-rounded-square-with-number-2-md.png','http://www.clker.com/cliparts/m/e/j/n/R/V/number-3-in-light-blue-rounded-square-md.png'],['http://www.clker.com/cliparts/X/b/l/a/X/w/red-rounded-square-with-number-3-md.png', 'http://www.clker.com/cliparts/p/7/W/8/F/T/purple-rounded-square-with-number-5-md.png', 'http://www.clker.com/cliparts/1/R/V/3/S/t/number-6-square-orange-md.png', 'http://www.clker.com/cliparts/g/I/c/I/S/3/red-rounded-square-with-number-7-md.png'], [ 'http://www.clker.com/cliparts/V/T/J/T/o/M/blue-rounded-square-with-number-8-md.png', 'http://www.clker.com/cliparts/B/f/Q/6/l/x/red-rounded-square-with-number-8-md.png', 'http://www.clker.com/cliparts/W/J/x/h/t/E/red-rounded-square-with-number-10-md.png', 'http://www.clker.com/cliparts/O/C/B/M/R/0/blue-rounded-square-with-number-11-md.png'], ['http://www.clker.com/cliparts/0/Y/a/p/2/W/hot-pink-rounded-square-with-number-12-md.png','http://www.clker.com/cliparts/V/X/Y/5/Y/t/red-rounded-square-with-number-13-th.png', 'http://www.clker.com/cliparts/W/H/D/y/K/5/green-rounded-square-with-number-14-md.png', 'http://www.clker.com/cliparts/q/l/a/c/m/x/purple-rounded-square-with-number-15-md.png']]; function generateTable(imgArray){ var body = document.getElementsByTagName("body")[0]; var tbl = document.createElement("table"); var tblBody = document.createElement("tbody"); for (var r = 0; r < 4; r++){ var row = document.createElement("tr"); for (var c = 0; c < 4; c++){ var cell = document.createElement("td"); // Call setImg() pass imgArray, current row and column index var cellImg = setImg(imgArray, r, c); cell.appendChild(cellImg); row.appendChild(cell); } tblBody.appendChild(row); } tbl.appendChild(tblBody); body.appendChild(tbl); } /* Pass a 3D array, current index numbers of row and column positions Create a <img> Add url and width return <img> */ function setImg(x3DArray, row, col) { var url = x3DArray[row][col]; var img = document.createElement('img'); img.src = url; img.style.width = '50px'; return img; } generateTable(imgArray); </script> </body> </html>来维护对其他Realm对象的引用。如果List不是Realm对象子类,则不能存储对其的引用。看到这里:https://realm.io/docs/swift/latest/#models

所以有两个选择:

  1. 或者将Module属性更改为modules并在Realm中维护List对象,或者
  2. 如果要存储JSON,是否需要存储Module属性?您可以继续仅存储JSON字符串,并使modules成为计算属性(为此您需要modules,并且可以选择是否创建getter) 。

您必须根据用例选择前进的方式。