【发布时间】:2015-11-25 06:38:27
【问题描述】:
我正在使用 Alamofire 和 SwiftyJSON 调用 API 并返回 JSON 响应。然后我正在使用:
let responseJSON = JSON(response.result.value!)
let userDataJSON = responseJSON["userData"]
Sync.changes(
userDataJSON,
inEntityNamed: "User",
dataStack: self.appDelegate.dataStack,
completion: { (response ) -> Void in
print("User \(response)")
})
尝试使用 Hyperoslo Sync 同步对 Core Data 的响应,但出现错误
Cannot convert value of type 'JSON' to expected argument type '[AnyObject]!'
谢谢
编辑
Alamofire.request(Router.AuthenticateUser(postParameters))
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
if response.result.isSuccess {
let responseJSON = JSON(response.result.value!)
if let userDataJSON = responseJSON["userData"] {
Sync.changes(
[userDataJSON],
inEntityNamed: "User",
dataStack: self.appDelegate.dataStack,
completion: { (response ) -> Void in
print("User \(response)")
})
}
...
Initializer for conditional binding must have Optional type, not 'JSON'
【问题讨论】:
-
我对 sn-p 或 SwiftyJSON 了解不多,但 JSON 不是字典而不是数组吗?
-
您的 userDataJSON 不是一个数组,您需要像 [userDataJSON] 一样传递参数,只需检查 userDataJSON 是否是可选的,然后使用 if let 或 force unwrap (!) 即 [userDataJSON] 安全地打开它!