Storing an array of strings using Realm's RLMArray

前端 未结 5 2138
天命终不由人
天命终不由人 2020-12-14 03:48

Does anyone know how you can use Realm to store an array of strings? I\'m trying to map the following response into Realm correctly:

\"zoneInfo\": {
    \"ta         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-14 04:11

    The RealmString approach is good, but you end up with a new RealmString every time you update the values, leaving a ton of unused objects laying around if you don't clean them up.

    I would suggest using something like:

    fileprivate let separator = "\u{FFFF}"
    
    class Person: Object {
        fileprivate dynamic var _nicknames: String?
        var nicknames: [String] {
            get { return _nicknames?.components(separatedBy: separator) ?? [] }
            set { _nicknames = newValue.isEmpty ? nil : newValue.joined(separator: separator) }
        }
    
        override static func ignoredProperties() -> [String] {
            return ["nicknames"]
        }
    }
    

提交回复
热议问题