2015-10-30 161 views
10

我是Swift新手,我跟着本教程:關於MapKit的http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial。問題是我在這行代碼上遇到錯誤無法將類型'[String:String?]'的值轉換爲期望的參數類型'[String:AnyObject?]'

let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict) 

該錯誤在標題上描述。包含此行的方法是:

func mapItem() -> MKMapItem { 
    let addressDict = [String(kABPersonAddressStreetKey): self.subtitle] 
    let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict) 

    let mapItem = MKMapItem(placemark: placemark) 
    mapItem.name = self.title 

    return mapItem 
} 

請幫忙。

回答

14

你需要投你subtitleAnyObject,如下圖所示:

let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject] 

和您的完整代碼將是:

func mapItem() -> MKMapItem { 
    let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject] 
    let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict) 

    let mapItem = MKMapItem(placemark: placemark) 
    mapItem.name = self.title 

    return mapItem 
    } 
+0

這就是它!非常感謝! – Adela

+0

我連根拔起,因爲我不快。 :)我不知道爲什麼它當我試圖投下字典時抱怨...這是它 –

+0

這有幫助。非常感謝你 – PersianBlue

相關問題