输入任何?!在swift 3中没有下标成员

时间:2017-06-01 12:42:44

标签: arrays dictionary swift3 xcode8

我将代码从2.3转换为swift 3.0时遇到问题 这是我的swift 2.3代码

.table-row-cell:border {
    -fx-border-color:green;
    -fx-border-width:2;
    -fx-opacity:1;
}

此代码在swift 2.3中正常工作但3.0显示错误。

  

输入任何?!没有下标成员。

1 个答案:

答案 0 :(得分:0)

之前, swift 2.3 result["rows"]![0]中的代码很容易编译而没有错误。但是在 swift 3 中,事情发生了变化,因为结果转换为Any类型而Any类型没有下标成员。

result["rows"]应首先在Array中进行类型转换,然后从该数组中可以访问第0个元素,这需要在您接收的类型中进一步进行类型转换。

如果您希望上述代码像以前一样工作,那么您可以使用SwiftyJSON将结果转换为JSON对象

假设你的结果是一个对象,里面有一个名为row的数组。

let dictionary = result as! [String:AnyObject]
let yourResultInJSON = JSON(rawValue: dictionary)

然后,您可以轻松使用此行yourResultInJSON["rows"]![0]["value"],它也是一个新的JSON对象,您可以在其中获取所需的键值,如:

let finalValue = yourResultInJSON["rows"]![0]["value"]
let someValue = finalValue["yourKey"].stringValue   //or intValue or whatever you need to typecast