【发布时间】:2018-10-12 22:47:57
【问题描述】:
我在下面代码的 sn-p 上遇到错误。每次我尝试构建它时,编译器都会抱怨:
不能用“String”类型的索引为“[NSObject : AnyObject]”类型的值下标
代码如下:
import Foundation
import MapKit
enum LocationKey: String {
case Latitude = "lat"
case Longitude = "long"
case Title = "title"
}
extension MKPointAnnotation {
var propertyState: [NSObject: AnyObject] {
get {
return [ LocationKey.Longitude.rawValue as NSObject: NSNumber(value: coordinate.latitude),
LocationKey.Longitude.rawValue as NSObject: NSNumber(value: coordinate.longitude),
LocationKey.Title.rawValue as NSObject: title as AnyObject]
}
set {
let lat = (newValue[LocationKey.Latitude.rawValue] as NSNumber).doubleValue
let long = (newValue[LocationKey.Longitude.rawValue] as NSNumber).doubleValue
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
title = newValue[LocationKey.Title.rawValue] as NSString
}
}
}
我真正遇到问题的代码行是:
let lat = (newValue[LocationKey.Latitude.rawValue] as NSNumber).doubleValue
let long = (newValue[LocationKey.Longitude.rawValue] as NSNumber).doubleValue
title = newValue[LocationKey.Title.rawValue] as NSString
非常感谢!
【问题讨论】:
标签: ios swift xcode dictionary mapkit