mike vorisis mike vorisis - 1 month ago 15
iOS Question

Add arrays to Realm with swift 3

I'm new in Realm and I tried to add an Array as I did with strings and I ended up with some errors. So after a little search I found out a solution:

class Sensors : Object {


dynamic var name = ""
dynamic var message = ""

var topic: [String] {
get {
return _backingNickNames.map { $0.stringValue }
}
set {
_backingNickNames.removeAll()
_backingNickNames.append(objectsIn: newValue.map({ RealmString(value: [$0]) }))
}
}
let _backingNickNames = List<RealmString>()

override static func ignoredProperties() -> [String] {
return ["topic"]
}




}

class RealmString: Object {
dynamic var stringValue = ""
}


This is working very good, now I want to add another array inside this class.
If someone knows any other ways to add arrays with realm please share it.

Thanks in advance

Answer

After testing I managed to add another array like that:

class Sensors : Object {


    dynamic var type = ""
    dynamic var name = ""
    dynamic var badge = 0

    var topic: [String] {
        get {
            return topicV.map { $0.stringValue }
        }
        set {
            topicV.removeAll()
            topicV.append(objectsIn: newValue.map({ RealmString(value: [$0]) }))
        }
    }

    var message: [String] {
        get {
            return messageV.map { $0.stringValue1 }
        }
        set {
            messageV.removeAll()
            messageV.append(objectsIn: newValue.map({ RealmString1(value: [$0]) }))
        }
    }





    let topicV = List<RealmString>()
    let messageV = List<RealmString1>()

    override static func ignoredProperties() -> [String] {
        return ["topic", "message"]
    }




}

class RealmString: Object {
    dynamic var stringValue = ""
}

class RealmString1: Object {
    dynamic var stringValue1 = ""
}
Comments