ios - 删除可选的 Alamofire(以及任何地方)

标签 ios swift alamofire

我正在尝试在 Swift 中实现新的 REST 服务,但我迷失了。我需要删除服务收据中的“可选”字符串:

func callServiceReceives(){

    var jsonReceived: JSON = []

    Alamofire.request(.GET, "http://localhost:8080/userServicesProvider")
        .responseJSON { (_, _, pruebaJSON, _) in

            jsonReceived = JSON(pruebaJSON!)
            println(jsonReceived["recibos"].array?.count) // "Optional(4)"
    }

}

//  If i try if let temp:String! = String(stringInterpolationSegment: jsonReceived["recibos"].array?.count) {
//  println(temp) -> "Optional(4)"
//  } -> say me "Bound value in a conditional binding must be of Optional type"

//If i try ->     let temp:String! = String(stringInterpolationSegment: jsonReceived["recibos"].array?.count)
//
//                if let temp2 = temp!{
//                    println(temp2) -> "Optional(4)"
//} -> i had the same problem: "Bound value in a conditional binding must be of Optional type"

你能帮帮我吗?

非常感谢

最佳答案

您将 ! 放在了错误的位置。它应该在这里:

println(jsonReceived["recibos"].array?.count!)

不过,为了安全起见,您最好:

println(jsonReceived["recibos"].array?.count.map{String($0)} ?? "Error")

如果你的数组为 nil,这将打印“错误”,或者:

jsonReceived["recibos"].array?.count.map(println)

如果您的数组为零,则不会打印任何内容。

关于ios - 删除可选的 Alamofire(以及任何地方),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137648/

相关文章:

ios - Xcode:禁用后启用Create Git Repository

ios - UITests 下无法点击 UIButton

ios - 在 Background Swift 中循环并将 json 数据附加到 UIImage[]

ios - 如何使用 Alamofire 发送带有 gzip 内容的 HTTP POST 请求?

ios - 尝试插入 10000 条记录 sqlite ios swift

iOS : Customizing TableViewCell - Initializing Custom Cell

objective-c - 如何从文件系统删除图像/文件?

ios - 使用分割 View Controller 使菜单项变灰 - Catalyst

ios - 如何更改选定的可扩展表格 View 单元格的背景颜色

ios - 字符 0 周围的 Alamofire 无效值