我在下面的代码片段中发现了错误。每次我试图构建它时,编译器都会抱怨:
不能用“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谢谢一堆人!
发布于 2018-05-02 18:37:21
你的代码太复杂了。所有值都是值类型,因此请将字典声明为[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,这将导致编译器错误。
发布于 2018-05-02 18:27:09
var propertyState: [NSObject: AnyObject]是一个字典,它有NSObject类型的键。尝试将其更改为键入String,并查看它是否有效。
发布于 2018-05-02 18:29:46
这是因为您正在创建一个带有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中看到了properyState选项,因为不使用强制展开是一种很好的做法。
https://stackoverflow.com/questions/50141190
复制相似问题