How do I convert swift Dictionary to NSDictionary
You have to cast it like this:
let dict = parcelDict as NSDictionary
Otherwise the Swift Dictionary and NSDictionary are treated almost the same way when using it in methods for ex:
func test(dict: NSDictionary) {}
let dict = ["Test":1]
test(dict)
Will work completely fine.
After your update
If you change your Dictionary value type to non optional String then your error will go away.
[String:String?] change to -> [String:String]
Convert Swift Dictionary to NSDictionary
Try parms.bridgeToObjectiveC()
. That may give you an NSDictionary rather than an NSMutableDictionary, though.
Swift Dictionary [String:String] to NSMutableDictionary?
There’s no built-in cast for this. But instead you can use NSMutableDictionary
’s initializer that takes a dictionary:
var foundationDictionary = NSMutableDictionary(dictionary: dictionary)
How do I convert NSDictionary to Dictionary?
NSDictionary
in Objective-C has always non-optional values.AnyObject
has becomeAny
in Swift 3.- Considering the first two "rules"
NSDictionary
can be bridge cast toDictionary
let post_paramsValue = post_params as Dictionary<String,Any>
If the source NSDictionary
is an optional you might use as Dictionary<String,Any>?
or as? Dictionary<String,Any>
or as! Dictionary<String,Any>
or as Dictionary<String,Any>!
depending on the actual type of the NSDictionary
Convert from NSDictionary to [String:Any?]
Hope this helps:
let dict = NSDictionary()
var anyDict = [String: Any?]()
for (value, key) in dict {
anyDict[key as! String] = value
}
convert JSON dictionary to NSDictionary
You can get an int or a string from SwiftyJSON objects by using the following functions: .stringValue, .intValue, .boolValue, .doubleValue etc.
Convert Swift Dictionary With Enum Value To NSDictionary
NSJSONSerialize
and friends can only deal with small subset of NSObject children (NSNumber, NSString, NSArray, NSDictionary, and NSNull) that correspond to the JSON native data types. In addition, a Dictionary can only be converted to an NSDictionary if both the key and value are NSObject or inherently convertible to NSObject (String and numeric types).
In order to serialize your Dictionary you'll need to convert the Enum to one of those NSJSONSerialize data-types, similar to your example:
enum MyEnum : String {
case one = "one"
case two = "two"
}
let dict = ["one":MyEnum.one, "two":MyEnum.two]
var newDict = Dictionary<String, String>()
for (key, val) in dict {
newDict[key] = val.rawValue
}
let data = NSJSONSerialization.dataWithJSONObject(newDict, options: .allZeros, error: nil)
As an alternative, since this kind of manipulation is fairly common, you might want to consider adding this category to Dictionary, which gives it a convenient map
function:
extension Dictionary {
init(_ pairs: [Element]) {
self.init()
for (k, v) in pairs {
self[k] = v
}
}
func map<K: Hashable, V>(transform: Element -> (K, V)) -> [K: V] {
return Dictionary<K, V>(Swift.map(self, transform))
}
}
Once that's done, the conversion is simply:
let mapped = dict.map { (key, value) in (key, value.rawValue) }
What is the difference between these two options for converting Swift Dictionary to NSMutableDictionary?
This works in most cases
No, this works not at all.
Unlike immutable NSDictionary
Swift Dictionary
and NSMutableDictionary
are not related.
You can bridge cast (as
without ?
) [String:String]
to NSDictionary
but you cannot bridge cast nor conditionally downcast (as?
) it to NSMutableDictionary
.
The second syntax works because a new instance of NSMutableDictionary
is created. The Swift dictionary parameter is implicitly bridged to NSDictionary
The same behavior is also true for Swift Array
and Foundation NSMutableArray
Related Topics
Passing Dynamic Int Variable from One Class to Another Class in Swift
Any or a Trouble with Sequence of Functions
Easiest Way to Increment a Data Point in Firebase
Write into Settings Bundle in Swift
Making Sklabelnode as a Crop Node of Skshapenode
How to Sum the Numbers(Int16) of Stored Core Data - Swift 3
Swift-Setting a Physics Body Velocity by Angle
How to Make a Segue to Second Item of Tab Bar
Firebase iOS Receive Data from Push Notification
How to Add 3D Shapes in Swift Ui
Non-Modular Headers of Openssl Library When Using Modulemap for Swift Framework
Swiftui View Does Not Updated When Observedobject Changed
C-Style Uninitialized Pointer Passing in Apple Swift
In Swift, Why Can't I Instantiate a Protocol When It Has an Initialiser
Swift: Differencebetween a Typealias and an Associatedtype with a Value in a Protocol