无法用索引类型“字符串"下标"[NSObject:AnyObject]"类型的值 [英] Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String'
问题描述
我在下面的代码片段中遇到错误.每当我尝试构建它时,编译器都会抱怨:
I been getting errors on the snippet of code below. Every time I try to build it, the compiler complains:
不能用索引类型为"String"的下标"[NSObject:AnyObject]"的值
Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type '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
}
}
}
我真正遇到麻烦的代码行是:
The lines of code that I really have trouble with are:
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]
–可以解决该错误–并消除对NSNumber
和AnyObject
的所有丑陋类型强制转换.
Your code is too complicated. All values are value type so declare the dictionary as [String:Any]
– which solves the error by the way – and get rid of all ugly type casts to NSNumber
and 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
}
}
}
您甚至可以将枚举用作键
You can even use the enum as key
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
}
}
}
注意:您的吸气剂使用LocationKey.Longitude
两次,这将导致编译器错误.
Note: Your getter uses LocationKey.Longitude
twice which will cause a compiler error.
这篇关于无法用索引类型“字符串"下标"[NSObject:AnyObject]"类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!