无法使用索引类型为“String”的类型”下标值

我在下面的代码片段中遇到错误。 每次我尝试构建它时,编译器都抱怨:

无法使用索引类型为“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 

谢谢你!

你的代码太复杂了。 所有值都是值类型,因此将字典声明为[String:Any] – 它解决了错误 – 并将所有丑陋的类型转换为NSNumberAnyObject

 import Foundation import MapKit enum LocationKey: String { case latitude = "lat" case longitude = "long" case title = "title" } extension MKPointAnnotation { var propertyState: [String: Any] { get { return [ LocationKey.latitude.rawValue: coordinate.latitude, LocationKey.longitude.rawValue: coordinate.longitude, LocationKey.title.rawValue: title ?? ""] } set { guard let lat = newValue[LocationKey.latitude.rawValue] as? CLLocationDegrees, let long = newValue[LocationKey.longitude.rawValue] as? CLLocationDegrees else { return } coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) title = newValue[LocationKey.title.rawValue] as? String } } } 

您甚至可以使用枚举作为键

 extension MKPointAnnotation { var propertyState: [LocationKey: Any] { get { return [ .latitude: coordinate.latitude, .longitude: coordinate.longitude, .title: title ?? ""] } set { guard let lat = newValue[.latitude] as? CLLocationDegrees, let long = newValue[.longitude] as? CLLocationDegrees else { return } coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) title = newValue[.title] as? String } } } 

注意:您的getter使用LocationKey.Longitude两次,这将导致编译器错误。

var propertyState: [NSObject: AnyObject]是一个包含NSObject类型键的Dictionary。 尝试将其更改为String类型,看看是否有效。

这是因为你正在用NSObject键创建一个Dictionary。

尝试这个:

 import Foundation import MapKit enum LocationKey: String { case Latitude = "lat" case Longitude = "long" case Title = "title" } extension MKPointAnnotation { var propertyState: [String: AnyObject] { get { return [ LocationKey.Longitude.rawValue: NSNumber(value: coordinate.latitude), LocationKey.Longitude.rawValue: NSNumber(value: coordinate.longitude), LocationKey.Title.rawValue: title as AnyObject] } set { guard let lat = (newValue[LocationKey.Latitude.rawValue] as? NSNumber)?.doubleValue, let long = (newValue[LocationKey.Longitude.rawValue] as? NSNumber)?.doubleValue else { return } coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) title = newValue[LocationKey.Title.rawValue] as? String } } } 

在代码中,您已经在properyState setter中看到了通常解包的选项,因为不使用force unwrapping是一种很好的做法。