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]?':
我想跳转到Xcode 7.3.1并转换我的代码,但是我在这里遇到了一些问题,这就是我在Swift 1.1中使用它的方式,但是我得到了错误-不能将‘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)
}
Thanks in Advance!
提前谢谢!
1 个解决方案
#1
1
Unlike immutable NSDictionary
the mutable NSMutableDictionary
is not related to a Swift Dictionary
and cannot be bridged nor casted.
与不可变的NSDictionary不同的是,可变的NSMutableDictionary与Swift字典没有关系,不能桥接或转换。
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
...
#1
1
Unlike immutable NSDictionary
the mutable NSMutableDictionary
is not related to a Swift Dictionary
and cannot be bridged nor casted.
与不可变的NSDictionary不同的是,可变的NSMutableDictionary与Swift字典没有关系,不能桥接或转换。
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
...