简体   繁体   中英

Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String: AnyObject]?'

i wanted to jump over to use Xcode 7.3.1 and convert my code, but I'm facing some kind of problem here,this is how i used to use it in Swift 1.1 but i am getting error -Cannot convert value of type 'NSMutableDictionary' to expected argument type '[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)
}

Thanks in Advance!

Unlike immutable NSDictionary the mutable NSMutableDictionary is not related to a Swift Dictionary and cannot be bridged nor casted.

There is a simple rule to avoid those problems:

In Swift use always Swift native collection types whenever possible.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM