Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类型any在swift 3中没有下标错误_Ios_Swift - Fatal编程技术网

Ios 类型any在swift 3中没有下标错误

Ios 类型any在swift 3中没有下标错误,ios,swift,Ios,Swift,当我将我的代码转换为当前的swift版本时,我面临着“Type any没有下标错误”。我对斯威夫特还不太熟悉,而且还停留在这一点上。看到了其他相关的帖子,但没有找到解决方法。请帮忙。我倒了很多,但找不到一个能清楚涵盖你的情况,所以我要解释一下发生了什么 您的变量属性明确声明为typeAny,这是一个可以保存任何类型的变量。错误消息告诉您不能使用订阅[]。很明显,您认为它是一个字典,其键是字符串,值可以是任何值。在Swift 3中,任何内容都是键入的Any。因此,您的属性应该是[String:An

当我将我的代码转换为当前的swift版本时,我面临着“Type any没有下标错误”。我对斯威夫特还不太熟悉,而且还停留在这一点上。看到了其他相关的帖子,但没有找到解决方法。请帮忙。

我倒了很多,但找不到一个能清楚涵盖你的情况,所以我要解释一下发生了什么

您的变量
属性
明确声明为type
Any
,这是一个可以保存任何类型的变量。错误消息告诉您不能使用订阅
[]
。很明显,您认为它是一个
字典
,其键是
字符串
,值可以是任何值。在Swift 3中,任何内容都是键入的
Any
。因此,您的
属性
应该是
[String:Any]
。如果将
属性
强制转换为
[String:Any]
,则可以将
[]
与之一起使用

您应该做的第一件事是尝试将
属性
强制转换为
[String:Any]
,然后继续,如果这样做有效:

    if let name = property["Name"] as? String,
      let latitude = property["Latitude"] as? NSNumber,
      let longitude = property["Longitude"] as? NSNumber,
      let image = property["Image"] as? String {

      // To learn how to read data from plist file, check out our Saving Data
      // in iOS Video Tutorial series.

      // Code goes here


    }

显示如何声明和初始化
属性的代码。您的答案可能重复,当然您的答案可以在…中找到。。。。另请注意:,
if let property = property as? [String : Any],
    let name = property["Name"] as? String,
    let latitude = property["Latitude"] as? NSNumber,
    let longitude = property["Longitude"] as? NSNumber,
    let image = property["Image"] as? String {

    // To learn how to read data from plist file, check out our Saving Data
    // in iOS Video Tutorial series.

    // Code goes here
}