【问题标题】:iOS8/Swift strange null behaviour with response from REST serviceiOS8/Swift 奇怪的空行为与来自 REST 服务的响应
【发布时间】:2015-02-12 17:16:58
【问题描述】:

在我的整个项目中,我可以完美地检查 Alamofire 的响应和数据

if fooData != nil {
//do stuff
}

在这种情况下,Swift 似乎在检查实际传入类型时遇到了问题

如果我打印它会得到什么

<null>

这应该是什么?一个空数组?强制转换为 NSDictionary 或 NSArray 均失败。

Rest 响应与在整个项目中始终相同,我可能会收到一个空值,并且它会在其他任何地方被捕获。

€dit 更多代码: 我的要求:

       Alamofire.request(.GET, "\(CurrentConfiguration.serverURL)/api/users/\(CurrentConfiguration.currentUser.id)/friends/\(targetUser)",encoding:.JSON)
            .validate()
            .responseJSON {(request, response, friendData, error) in

    if friendData != nil {
     println(friendData!)

     //this is where the app obviously crashes as there is nothing inside of "friendData"
     let resp = friendData as NSDictionary 


    //load friendData into the UI
    }

}

print 语句给了我上面提到的 null 表示,但显然不能识别为 nil

来自节点后端的响应是

index.sendJsonResponse(res, 200, null);

【问题讨论】:

  • 你能发布你的实际代码吗?
  • 我同意,如果您没有代码,这将很难提供帮助。
  • 用代码编辑了第一篇文章

标签: ios rest swift alamofire


【解决方案1】:

在 Playground 中试试这个代码:

let fooData:AnyObject = NSNull()
println(fooData)

它打印&lt;null&gt;

fooData 不是nil,而是NSNull 的一个实例

【讨论】:

  • 谢谢 - 我想我会检查“如果friendData是NSNull”然后
  • 添加一点信息,当 JSON 包含空值 ('null') 时会发生这种情况
  • 那么正确的空响应是什么?像我上面粘贴的那样响应似乎对于 angular 前端以及 android 应用程序来说都很好 - 检查“== null”
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多