输入' NSDictionary.Iterator.Element' (又名'(关键:任意,价值:任何)')没有下标成员

时间:2016-12-18 19:19:07

标签: ios swift swift3 nsdictionary

以下函数我遍历plist中的项目,这在Swift 1中工作得很好,可能是Swift 2,但绝对不是Swift 3.在尝试将NSDictionary重新定义为Dictionary之后,它会询问类型将存储在字典中,但在尝试不同的变体(如字符串甚至任何变体)时,它不会编译。因此,我转而将其保留为NSDictionary,但这个错误令我感到难过:

sw $v0,array($t0)

设置常量时会出现此错误:point,title,typeRawValue和subtitle。

Type 'NSDictionary.Iterator.Element' (aka '(key: Any, value: Any)') has no subscript members

以下是plist的屏幕截图: enter image description here 如果您需要进一步的代码来了解正在发生的事情,请不要犹豫,并提前感谢任何人的帮助。

1 个答案:

答案 0 :(得分:1)

我不确定,您的上一条评论意味着您已成功转换代码,但看到了plist的图像,您的代码可以重写为:

    if
        let filePath = Bundle.main.path(forResource: "GeorgiaHumanAnnotations", ofType: "plist"),
        let dictArray = NSArray(contentsOfFile: filePath) as? [[String: Any]]
    {
        for attraction in dictArray {
            let point = CGPointFromString(attraction["location"] as! String)
            let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(point.x), CLLocationDegrees(point.y))
            let title = attraction["name"] as! String
            let typeRawValue = Int((attraction["type"] as! String))!
            let type = HumanAnnotationType(rawValue: typeRawValue)!
            let subtitle = attraction["subtitle"] as! String
            let annotation = HumanAnnotation(coordinate: coordinate, title: title, subtitle: subtitle, type: type)
            mapView.addAnnotation(annotation)
        }
    }
相关问题