ios - 需要帮助声明数组以在 Swift 3 中检索 plist 信息

标签 ios arrays swift swift3

<分区>

由于从 Swift 2 到 Swift 3 的过渡刚刚发生的变化,我无法找出 Xcode 上我的代码问题的解决方案。

 func addBuildingPins(){
    let filePath = Bundle.main.path(forResource: "CurtinBuildingList", ofType: "plist")
    let buildings = NSMutableArray(contentsOfFile: (filePath)!) /*check plist file to see if it is a dictionary or array or else for loop cannot run*/
    for building in buildings!{
        let point = CGPointFromString(building["coordinate"] as! String)
        let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(point.x), CLLocationDegrees(point.y))
        let title = building["title"] as! String
        let typeRawValue = Int(building["type"] as! String)!
        let type = BuildingType(rawValue: typeRawValue)
        let subtitle = building["subtitle"] as! String
        let annotation = BuildingAnnotation(coordinate: coordinate, title: title, subtitle: subtitle, type: type!)
        mapView.addAnnotation(annotation)
        print(building)
    }
}

错误就在方括号 (building[]) 之前。我不断收到的错误是

Type 'NSFastEnumerationIterator.Element' (aka, Any) has no subscript members".

有什么办法解决这个问题吗?

最佳答案

在使用 for 循环之前将建筑物数组转换为 [[String: Any]]

if let array = buildings as? [[String: Any]] {
     for building in array {
          //Your code 
     }
}

关于ios - 需要帮助声明数组以在 Swift 3 中检索 plist 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761917/

相关文章:

iphone - 通过 tableview 添加 View (UITableViewController)

ios - 如何将 json 数据从 alamofire 转换为 swift 对象

javascript - 按键组合对象数组

Java:在我的程序中将文件数据转换为可用数据时遇到问题。有小费吗?

ios - 导航栏大标题到小标题切换不流畅 iOS 13,粘性

ios - 在被点击的单元格的索引处更改数据源

ios - UIWebView上的PDF

objective-c - 动画 UIView 框架, subview UIScrollView 并不总是动画

php - 如何在不循环执行的情况下将数组插入mysql

swift - 如何在类中定义 "inline struct"而无需先在 swift (以及更多)中声明它?