Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array of Dictionary mapping #1118

Open
sam961 opened this issue Apr 22, 2021 · 0 comments
Open

Array of Dictionary mapping #1118

sam961 opened this issue Apr 22, 2021 · 0 comments

Comments

@sam961
Copy link

sam961 commented Apr 22, 2021

I have the below model and i need to map the array of object directly to the model without creating two models

one for 'childActivityList'
and the other for nested array object

can i take the values directly?

childId <- map["childActivityList.0.childId"]

The above will always take the first value

In other words...i need to get rid of childActivityList

Your JSON dictionary:

  "data": {
        "childActivityList": [
            {
                "childId": "34bad8a5-0315-4d9f-a948-4780ed266040",
                "childName": "Test Produkte",
                "childImage": "https://testurl/s.png",
                "appName": "TikTok Lite",
                "appPackageName": "com.zhiliaoapp.musically.go",
                "appTime": "2167120",
                "openTime": "1619012403405",
                "deviceName": "Test Produkte_Android Tablet",
                "appIcon": null,
                "location": "Tirana",
                "isBlocked": false,
                "message": "now active on TikTok Lite"
            }
}

Your model:

class ActivityModel: Mappable, Encodable {
    
    var appName : String?
    var appPackageName : String?
    var appTime : String?
    var appIconURL:String?
    var deviceName : String?
    var id : String?
    var location : String?
    var openTime : String?
    var profileImage : String?
    var childName : String?
    var childImage : String?
    var message : String?
    
    required init?(map: Map) {

    }

    func mapping(map: Map) {

        appName <- map["appName"]
        appPackageName <- map["appPackageName"]
        appTime <- map["appTime"]
        deviceName <- map["deviceName"]
        id <- map["id"]
        location <- map["location"]
        openTime <- map["openTime"]
        profileImage <- map["profileImage"]
        appIconURL <- map["appIcon"]
        childName <- map["childName"]
}

What you did:

let activities = baseModel?.data?.childActivityList

What you expected:

I exepected something like:

Correct Model

What you got:

nil values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant