无法使用类型为JSON的参数列表调用类型为"Int"的初始值设定项

Ork*_*ade 3 json ios swift

为什么我无法将JSON响应中的String值转换为Int?它返回下一个错误:

Cannot invoke initializer for type 'Int' with an argument list of type '(JSON)'
Run Code Online (Sandbox Code Playgroud)

当我尝试:

Int(json[i]["id"])
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它转换为Int?

dnl*_*ggr 8

我假设您使用的是SwiftyJSON.

在这种情况下,您有两个选择:

可选的getter:

if let id = json[i]["id"].int {
   // do something
}
Run Code Online (Sandbox Code Playgroud)

非可选的getter:

let id: Int = json[i]["id"].intValue
Run Code Online (Sandbox Code Playgroud)