简体   繁体   English

NSFastEnumerationIteration.member Swift 3

[英]NSFastEnumerationIteration.member Swift 3

I have some difficult to convert my Swift 2.2 app to Swift 3.0. 我很难将Swift 2.2应用程序转换为Swift 3.0。 I have some errors and I don't find the solution yet. 我有一些错误,但找不到解决方案。 Currently, my worst problem is with NSFastEnumerationIteration , I try to get records from JSON but with this error I can't. 目前,我最严重的问题是NSFastEnumerationIteration ,我尝试从JSON获取记录,但是由于这个错误,我无法实现。 This is the screenshot of my code with the problem : 这是我的代码截图与问题:

NSFastEnumerationInteration的Swift 3.0错误

In Swift 3 you need to specify the type of object,so specify the type of your data Array to [[String:Any]] . 在Swift 3中,您需要指定对象的类型,因此将数据数组的类型指定为[[String:Any]]

if let dataArr = data as? [[String: Any]] {
    for dd in dataArr {
        //your code for accessing dd.
    }
}

For in loop only knows that your variable data is an array and doesn't know anything else, so you need to provide as well the type of the content of your variable data : 因为in 循环仅知道您的变量 数据是一个数组,并且不知道其他任何内容,因此您还需要提供变量数据 内容的类型

let dataToParse = dataweneed.data(using: String.Encoding.utf8.rawValue)!
let jsonOptions = [JSONSerialization.ReadingOptions.mutableContainers]
let data = try JSONSerialization.jsonObject(with: dataToParse, options: jsonOptions)

// now For in loop would know that you
// could have an array of dictionaries
if let data = data as? [[String: Any]] {
  for dd in data {
    // your code
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM