我在下面的代码片段中遇到错误。每次我尝试构建它时,编译器都会抱怨:
无法下标类型' [NSObject:AnyObject]'索引类型为' String'
这是代码中的所有荣耀:
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
非常感谢!
答案 0 :(得分:1)
var propertyState: [NSObject: AnyObject]
是一个包含NSObject
类型键的词典。尝试将其更改为键入String
并查看是否有效。
答案 1 :(得分:1)
这是因为你正在用NSObject
键创建一个词典。
试试这个:
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中看到了сonditionally unwrapped个选项,因为不使用强制解包是一种不错的做法。
答案 2 :(得分:1)
您的代码太复杂了。所有值都是值类型,因此将字典声明为[String:Any]
- 这样可以解决错误 - 并将所有丑陋的类型转换为NSNumber
和AnyObject
。
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
两次,这将导致编译器错误。