2

I get an error when I try to save an array that comes from a JSON string. I've tried to use RLMArray with no success.

The error I receive is:

'RLMException', reason: 'Property 'page' is of type 'RLMArray<(null)>' which is not a supported RLMArray object type.

My model class:

public class Project: Object, Mappable {

    dynamic var id = 0
    dynamic var user: User!
    dynamic var page: RLMArray!
    dynamic var error_message: String! = ""
    dynamic var status: String! = ""

    override public static func primaryKey() -> String? {
        return "id"
    }

    required convenience public init?(_ map: Map) {
        self.init()
        mapping(map)
    }

    public func mapping(map: Map) {
        user <- map["user"]
        page <- map["page"]
        error_message <- map["error_message"]
        status <- map["status"]

    }
}

JSON File:

let parameters = [
                "user": [
                    "username": "Marcus",
                    "password": "123asd"
                ],
                "page": [
                    "home": [
                                [
                                "kind": "navigation",
                                "title": "suite",
                                "image": "ic_suite",
                                "backgroundImage": "ic_background1"
                                ],
                                [
                                    "kind": "navigation",
                                    "title": "jardim",
                                    "image": "ic_jardim",
                                    "backgroundImage": "ic_background2"
                                ]
                            ],
                    "suite": [
                                [
                                "kind": "button",
                                "title": "My Master Suite",
                                "textColor": "0x000000",
                                "textSize": "16"
                                ]
                    ]
                ],
                "status": "success",
                "error_message": ""
        ]
self.project = Mapper<Project>().map(parameters)
Marckaraujo
  • 7,422
  • 11
  • 59
  • 97

1 Answers1

2

Your class inherits from Object, Realm Swift's base class, but is attempting to use RLMArray, a Realm Objective-C type, in its interface. You cannot mix Realm Swift and Realm Objective-C in this manner. You should use List<T> for array properties when using Realm Swift.

bdash
  • 18,110
  • 1
  • 59
  • 91
  • I tried to use List but got: Property cannot be marked dynamic because its type cannot be represented in Objective-C – Marckaraujo Jul 21 '16 at 23:22
  • `List` properties should be declared as `let property = List()`. See [Realm's property declaration cheatsheet](https://realm.io/docs/swift/latest/#cheatsheet) for info. – bdash Jul 22 '16 at 03:57
  • Thanks @bdash, but I cant use List(), if you saw, my arrayList have different types of Objects. – Marckaraujo Jul 22 '16 at 14:35
  • Realm's List type is a homogenous container, i.e., it only supports storing objects of the same type. If you want to store objects of different types you'll need to either use multiple lists, or a list containing a wrapper type that has a to-one relationship with each of the types you'd like to store in the list. – bdash Jul 22 '16 at 14:47
  • you're correct! Do you have a hint of how can I store this json using RealmSwift and ObjectMapper? – Marckaraujo Jul 23 '16 at 02:25