如何在 swift 4 中使用对象映射器附加字典数组?

How to append a array of dictionary using object mapper in swift 4?

成功登录后,我在控制台中收到此消息。在用户内部有一个名为 UserMedias 的数组列表。我想将 UserMedias 附加到一个空数组列表,以便我可以在 table 视图中显示 UserMedias 的数据。我只使用对象映射器和 Swift 4.

var messageList : [UserMedias] = []

请告诉我如何将 UserMedias 的数据追加到名为 messageList 的空数组中。

// First store the "user_medias" array in a temporary array

let tempArr = resultDictionary["user_medias"] as! Array <Any>

// Then run this for loop 

for obj in tempArr
{
 let tempDic = obj as! Dictionary < String,Any> 
 emptyArray.append(tempDic)  
 // here emptyArray is the array where you want to append the objects
}

您需要像这样创建对象,

class UserRoot: BaseModel {
    var status: Bool?
    var message: String?
    var userDetails: UserDetails!

    override func mapping(map: Map) {
        message <- map["error"]
        userDetails <- map["user"]
        status <- map["success"]
    }
}

class UserDetails: BaseModel {
    var id : Int?
    var userMedias : [UserMedias] = []

    override func mapping(map: Map) {
        id <- map["id"]
        userMedias <- map["user_medias"]
    }
}

class UserMedia: BaseModel {
    var mediaId : Int?

    override func mapping(map: Map) {
        mediaId <- map["mediaId"]
    }
}

仅供参考。代码将根据您的要求进行更改。请进行必要的更改。根据需要创建属性。

如有任何疑问,请告诉我。