以下函数我遍历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
答案 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)
}
}