1

I'm currently trying to implement mapping to my models and the current way I'm doing it isn't yielding any result. I have a MediaLibrary class that holds 2 arrays, 'exerciseMedia' and 'stretchMedia' that both hold an array of 'Media' objects. I've opted for the ObjectMapper/AlamofireObjectMapper library to map data to my models.

import ObjectMapper

class MediaLibrary: Mappable {

    var exerciseMedia: [Media]?
    var stretchMedia: [Media]?

    required init?(map: Map) {
        mapping(map: map)
    }

    func mapping(map: Map) {
        exerciseMedia <- map["ExerciseMedia"]
        stretchMedia  <- map["StretchMedia"]
    }
}

Here is the Media object that will make up the objects populating both of my arrays in MediaLibrary class.

import ObjectMapper

struct Media: Mappable {

    var _id: String?
    var name: String?
    var desc: String?
    var imageURI: String?
    var videoURI: String?
    var trainerID: String?
    var isShoulders : Bool?
    var isNeck : Bool?
    var isLegs : Bool?
    var isHands : Bool?
    var isChest : Bool?
    var isCalves : Bool?
    var isButt : Bool?
    var isBack : Bool?
    var isArms : Bool?
    var isCore : Bool?
    var isFullBody : Bool?
    var isLowerBody : Bool?
    var isUpperBody : Bool?
    var isExercise : Bool?
    var isStretch : Bool?
    var equipmentNeeded : String?

    init?(map: Map){ }

    mutating func mapping(map: Map) {

        _id             <- map["_id"]
        name            <- map["name"]
        desc            <- map["desc"]
        imageURI        <- map["imageURI"]
        videoURI        <- map["videoURI"]
        trainerID       <- map["trainerID"]
        isShoulders     <- map["isShoulders"]
        isNeck          <- map["isNeck"]
        isLegs          <- map["isLegs"]
        isHands         <- map["isHands"]
        isChest         <- map["isChest"]
        isCalves        <- map["isCalves"]
        isButt          <- map["isButt"]
        isBack          <- map["isBack"]
        isArms          <- map["isArms"]
        isCore          <- map["isCore"]
        isFullBody      <- map["isFullBody"]
        isLowerBody     <- map["isLowerBody"]
        isUpperBody     <- map["isUpperBody"]
        isExercise      <- map["isExercise"]
        isStretch       <- map["isStretch"]
        equipmentNeeded <- map["equipmentNeeded"]
    }
}

Now here is a sample of the json I'm trying to map. Note my model has both 'imageURI' and 'videoURI' as there are 2 types of Media objects but since they're optionals it can account for this.

{
    "StretchMedia": [
        {
            "_id": "5a380db7ee292409b4f44980",
            "name": "somephoto",
            "desc": "some description",
            "imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100006166323",
            "trainerID": "5a32f62a28ce4acbc5fbbb4b",
            "__v": 0,
            "createdAt": "2017-12-18T18:49:27.009Z",
            "isShoulders": false,
            "isNeck": false,
            "isLegs": false,
            "isHands": false,
            "isChest": false,
            "isCalves": false,
            "isButt": false,
            "isBack": false,
            "isArms": false,
            "isCore": false,
            "isFullBody": false,
            "isLowerBody": false,
            "isUpperBody": false,
            "isExercise": false,
            "isStretch": true,
            "equipmentNeeded": "none"
        },
        {
            "_id": "5a380dfa089b2d09dcfa92db",
            "name": "somephoto",
            "desc": "some description",
            "videoURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100002098677",
            "trainerID": "5a32f62a28ce4acbc5fbbb4b",
            "__v": 0,
            "createdAt": "2017-12-18T18:50:34.277Z",
            "isShoulders": false,
            "isNeck": false,
            "isLegs": false,
            "isHands": false,
            "isChest": false,
            "isCalves": false,
            "isButt": false,
            "isBack": false,
            "isArms": false,
            "isCore": false,
            "isFullBody": false,
            "isLowerBody": false,
            "isUpperBody": false,
            "isExercise": false,
            "isStretch": true,
            "equipmentNeeded": "none"
        }
    ],
    "ExerciseMedia": [
        {
            "_id": "5a345a356ec5fc0032651611",
            "name": "Juan",
            "desc": "pic of some Spanish guy",
            "imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/10000998839",
            "trainerID": "5a32f62a28ce4acbc5fbbb4b",
            "isStretchCore": false,
            "isExerciseCore": false,
            "__v": 0,
            "createdAt": "2017-12-15T23:26:45.461Z",
            "isShoulders": false,
            "isNeck": false,
            "isLegs": false,
            "isHands": false,
            "isChest": false,
            "isCalves": false,
            "isButt": false,
            "isBack": false,
            "isArms": false,
            "isCore": false,
            "isFullBody": true,
            "isLowerBody": false,
            "isUpperBody": false,
            "isExercise": true,
            "isStretch": false,
            "equipmentNeeded": "Dumbbells"
        }
    ],
}

The problem I'm having is how to populate the 2 arrays on my MediaLibrary class with the contents of "ExerciseMedia" and "StretchMedia" to their respective arrays. I've created a function that makes a request (the body only needs the trainer id) to the route that contains the JSON you just saw. I'll post my code so far.

import AlamofireObjectMapper

var mediaLibrary: MediaLibrary?

func setTrainerMediaLibrary(trainerID: String) {
        let body: [String: Any] = [
            "trainerID": trainerID
        ]

        Alamofire.request(TRAINER_MEDIA_ALL, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseObject { (response: DataResponse<MediaLibrary>) in
            if response.result.error == nil {
                self.mediaLibrary = Mapper<MediaLibrary>().map(JSONObject: response.result.value)
            } else {
                print("Could not map JSON data to Media Library model")
                debugPrint(response.result.error as Any)
            }
        }
    }

The MediaLibrary object returns nil so something isn't right. My thoughts are that since the MediaLibrary has mapping for both "ExerciseMedia" and "StretchMedia" that all I need to do is use the MediaLibrary in the request and the mapping function will automatically handle the 2 media keys you see in the JSON. I could be going about this completely wrong but I know I'm close, any help would be appreciated.

1 Answer 1

1

As for the usage of AlamofireObjectMapper, the object response already mapped to the json. All you need just called as .value and ready to use.

self.mediaLibrary = response.result.value

The usage with Mapper<MediaLibrary>().map() is referring to ObjectMapper library itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was so simple but very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.