I'm doing the same operation again and again, namely converting a dictionary to JSON and NSData. Using Dan Kogai's JSON class https://github.com/dankogai/swift-json, I want to create an extension as follows:
extension Dictionary {
func toDataJSON() -> NSData? {
return JSON(self as Dictionary<String, AnyObject>)
.toString(pretty: false)
.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
}
}
I realize that not all Dictionary<key, value> sets are compatible with JSON, but for the sensible combination like Dictionary<String, AnyObject>I would like the above to work.
The compiler says:
'Key' is not identical to 'String'
Without casting self to Dictionary<String, AnyObject> it says:
Type 'Dictionary' does not conform to protocol 'AnyObject'
Any insights?