休息套件RKMappingOperation不起作用

时间:2015-10-15 04:44:44

标签: ios swift rest restkit

休息套件RKMappingOperation不起作用

这是我运行的代码。它应该在CvvName对象中创建数据。但它不起作用。如何让它发挥作用?

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) +")
}

打印空字符串

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

我想要它打印值

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

1 个答案:

答案 0 :(得分:1)

看起来问题出在您提供的源数据中:

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

这是一个字典数组,但您应该提供一个包含所有详细信息的字典,因为操作期望单个项目和单个项目输出。考虑:

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