Swift RestKit Post 不包括帖子数据

Posted

技术标签:

【中文标题】Swift RestKit Post 不包括帖子数据【英文标题】:Swift RestKit Post not including post data 【发布时间】:2015-04-14 15:39:31 【问题描述】:

我无法使用以下代码获取发布的数据。我已经尝试过所有关于 SO 的相关帖子,但仍然无法正常工作。

在请求描述符中添加,具有用于映射参数的 NSDictionary,尝试过 inverseMapping 等。

var parms = WearRequestParms()
parms.Height = height
parms.Width = width
parms.Density = density
if let userId = AlpineMetricsHttpClient.GetUserId()

     parms.UserId = userId


var objectManager : RKObjectManager?
objectManager = AlpineMetricsHttpClient.SetupClient()

// var mapping = RKObjectMapping(forClass:WearRequestParms.self)
var mapping = RKObjectMapping.requestMapping()

let requestMappingObjects =  ["IsCircle","Height","Width","Density","UserId","WearModel","Platform"]

let dict : NSMutableDictionary = ["IsCircle":"IsCircle","Height":"Height","Width":"Width","Density":"Density","UserId":"UserId","WearModel":"WearModel","Platform":"Platform"]

mapping.addAttributeMappingsFromArray(requestMappingObjects)
// mapping.addAttributeMappingsFromDictionary(dict as [NSObject : AnyObject])

// var newRequestMapping =  mapping.inverseMapping()

let requestDescriptor = RKRequestDescriptor(mapping: mapping, objectClass: WearRequestParms.self, rootKeyPath: "", method: RKRequestMethod.POST)

objectManager!.addRequestDescriptor(requestDescriptor)

// response
let responseMapping = RKObjectMapping(forClass:PostStatus.self)

var responseDescriptor = RKResponseDescriptor(
    mapping: responseMapping
    ,method:RKRequestMethod.POST
    ,pathPattern : "/api/User/RegisterWearDevice"
    ,keyPath :""
    ,statusCodes  :  NSIndexSet(index:200))

objectManager!.addResponseDescriptor(responseDescriptor)

RKObjectManager.sharedManager().postObject(parms, path: "/api/User/RegisterWearDevice", parameters: nil,
    success: operation, mappingResult in
        NSLog("success")

        defaults.setBool(true, forKey: "WearDimensionsSynced")

    ,
    failure: operation, error in
        NSLog("Error loading metric list': \(error!.localizedDescription)")
        //return nil
    
);

【问题讨论】:

【参考方案1】:

我不应该使用 RKObjectManager.sharedManager().postObject

这会导致之前的值被覆盖或干扰。

我换了

RKObjectManager.sharedManager().postObject

objectManager!.postObject

【讨论】:

以什么方式?你的意思是你有多个经理,所以你应该使用objectManager而不是RKObjectManager.sharedManager @Wain 正确,我用我切换的内容更新了我的答案。

以上是关于Swift RestKit Post 不包括帖子数据的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 关系问题

Restkit 版本 0.20.0-pre2。 : 问题:post object 中没有键。如何发布?

RestKit 不喜欢为 POST 响应映射不同的对象

RestKit 0.20.0 rc1 - 使用字符串数组创建请求帖子

如何从 Restkit + iOS 发布到 Rails

RestKit POST 发送 JSON 不包含映射路径/类名