无法转换类型的价值' NSMutableDictionary'预期的参数类型' [String:AnyObject]?'

时间:2016-07-09 10:57:24

标签: ios swift core-location

我想跳过使用Xcode 7.3.1并转换我的代码,但是我在这里遇到了某种问题,这就是我以前在Swift 1.1中使用它的方法但我得到的错误-Cannot转换类型' NSMutableDictionary'的值预期的参数类型' [String:AnyObject]?':

private func getPlacemark() -> CLPlacemark
  {

    var addressDict = NSMutableDictionary()
    var formattedAddressArray = self.formattedAddress.componentsSeparatedByString(", ") as Array

    let kSubAdministrativeArea = "SubAdministrativeArea"
    let kSubLocality           = "SubLocality"
    let kState                 = "State"
    let kStreet                = "Street"
    let kThoroughfare          = "Thoroughfare"
    let kFormattedAddressLines = "FormattedAddressLines"
    let kSubThoroughfare       = "SubThoroughfare"
    let kPostCodeExtension     = "PostCodeExtension"
    let kCity                  = "City"
    let kZIP                   = "ZIP"
    let kCountry               = "Country"
    let kCountryCode           = "CountryCode"

    addressDict.setObject(self.subAdministrativeArea, forKey: kSubAdministrativeArea)
    addressDict.setObject(self.subLocality, forKey: kSubLocality)
    addressDict.setObject(self.administrativeAreaCode, forKey: kState)

    addressDict.setObject(formattedAddressArray.first! as NSString, forKey: kStreet)
    addressDict.setObject(self.thoroughfare, forKey: kThoroughfare)
    addressDict.setObject(formattedAddressArray, forKey: kFormattedAddressLines)
    addressDict.setObject(self.subThoroughfare, forKey: kSubThoroughfare)
    addressDict.setObject("", forKey: kPostCodeExtension)
    addressDict.setObject(self.locality, forKey: kCity)


    addressDict.setObject(self.postalCode, forKey: kZIP)
    addressDict.setObject(self.country, forKey: kCountry)
    addressDict.setObject(self.ISOcountryCode, forKey: kCountryCode)


    var lat = self.latitude.doubleValue
    var lng = self.longitude.doubleValue
    var coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lng)

  var placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDict ) <-- getting error //Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String: AnyObject]?'


    return (placemark as CLPlacemark)
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

与不可变NSDictionary不同,可变NSMutableDictionary与Swift Dictionary无关,无法桥接或转换。

有一个简单的规则来避免这些问题:

在Swift中,尽可能使用Swift本机集合类型。

var addressDict = [String:AnyObject]()
...
addressDict[kSubAdministrativeArea] = self.subAdministrativeArea
...