niks290192 niks290192 - 1 year ago 106
Swift Question

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(, 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!

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download