![](/img/trans.png)
[英]Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String'
[英]Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type '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
謝謝一群!
var propertyState: [NSObject: AnyObject]
是一個字典,具有NSObject
類型的鍵。 嘗試將其更改為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中看到了按標准方式展開的可選內容,因為不使用強制展開是一種好習慣。
您的代碼太復雜了。 所有值都是值類型,因此將字典聲明為[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
,這將導致編譯器錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.