2016-01-10 82 views
1

我單身外景經理的職位,每當新的位置數據被內部locationManager(_:didUpdateLocations:)檢索通知:CLLocationCoordinate2D不符合預期的字典值類型「AnyObject」

... 
NSNotificationCenter.defaultCenter().postNotificationName("LocationUpdate", object: self, userInfo: ["currentLocation": currentLocation, 
                             "currentLocationPoints": currentLocationPoints])` 
... 

通過上述通知發佈,我發現了以下對於userInfo部分錯誤:

Value of type 'CLLocationCoordinate2D! does not conform to expected dictionary value type 'AnyObject'` 

因此,我提出了以下類型轉換:

userInfo: ["currentLocation": currentLocation as! AnyObject, 
      "currentLocationPoints": currentLocationPoints as! AnyObject] 

而這一次運行時,我發現了以下錯誤:

Could not cast value of type '__C.CLLocationCoordinate2D' (0xbb1f0) to 'Swift.AnyObject' (0x312d074). 

爲什麼鑄造失敗?我知道AnyAnyObject之間的區別AnyObject不能是一個枚舉或等,但CLLocationCoordinate2D應該罰款鑄造。

回答

2

CLLocationCoordinate2Dstruct並且不符合AnyObject協議。因此它不能存儲到字典中。

一個可能的解決方案是分別發佈Doublelatitudelongitude

另一種解決方案是鏈接到MapKit.framework和使用的NSValue擴展(這符合AnyObject

集:

let value = NSValue(MKCoordinate: currentLocation) 
dictionary["currentLocation"] = value 

得到:

let value = dictionary["currentLocation"] as! NSValue 
let currentLocation = value.MKCoordinateValue 
+0

哎呀,是我不好。感謝您指點我正確的方向。 –

相關問題