Search code examples
iosswiftrestrestkit

Rest kit RKMappingOperation do not work


Rest kit RKMappingOperation do not work

Here is code that I run. It should create data in CvvName object . but it do not work. how to make it work?

class CvvName: NSObject{
    var firstName = ""
    var lastName = ""
}


class CvvMappingProvider: NSObject
{
    class func nameMapping() -> RKMapping
    {
        let resultMapping : RKObjectMapping = RKObjectMapping(forClass: CvvName.classForCoder())
        resultMapping.addAttributeMappingsFromDictionary(["firstName":"firstName", "lastName": "lastName"])
        return resultMapping
    }
}

func doRestKit()
    {
        var theMapping:RKMapping = CvvMappingProvider.nameMapping()
let theRepresentation = [["firstName": "firstName01"] ,["lastName": "lastName01"] ]
        let theDestinationObject = CvvName()
        let theMappingOperation = RKMappingOperation(sourceObject: theRepresentation, destinationObject: theDestinationObject, mapping: theMapping)
        do {
            let theTry = try theMappingOperation.performMapping()
            //theMappingOperation.start()
        }
        catch is NSError {
            // Unexpected error!
        }



 print("theDestinationObject \(theDestinationObject) + \(theDestinationObject.firstName) + \(theDestinationObject.lastName) +")
}

it prints empty strings

theDestinationObject <ap01.CvvName: 0x7c20f260> +  +  +

I want that it print values

theDestinationObject <ap01.CvvName: 0x7c20f260> +   firstName01 +  lastName01 +

Solution

  • Looks like the problem is in the source data you're supplying:

    let theRepresentation = [["firstName": "firstName01"] ,["lastName": "lastName01"] ]
    

    this is an array of dictionaries, but you should really be supplying a single dictionary with all of the details because the operation is expecting a single item in and a single item out. Consider:

    let theRepresentation = ["firstName": "firstName01", "lastName": "lastName01"]