简体   繁体   English

如何将swift Dictionary转换为NSDictionary

[英]How do I convert swift Dictionary to NSDictionary

I'm trying to convert a [String : String] (a Swift Dictionary) to NSDictionary , for later use on a JSON library that produces a string 我正在尝试将[String : String] (一个Swift Dictionary)转换为NSDictionary ,以便稍后在产生字符串的JSON库上使用

var parcelDict = ["trackingNumber" : parcel.number,
                  "dstCountry" : parcel.countryCode];

if (parcel.postalService != nil) 
{
    parcelDict["postalService"] = parcel.postalService;
}

var errorPtr: NSErrorPointer
let dict: NSDictionary = parcelDict
var data = NSJSONSerialization.dataWithJSONObject(dict, options:0, error: errorPtr) as NSData

return NSString(data: data, encoding: NSUTF8StringEncoding)

but let dict: NSDictionary = parcelDict does not work 但是let dict: NSDictionary = parcelDict不起作用

let dict: NSDictionary = parcelDict as NSDictionary

var data = NSJSONSerialization.dataWithJSONObject(parcelDict as NSMutableDictionary, options:0, error: errorPtr) as NSData

All of these examples do not work. 所有这些例子都不起作用。 They produce the following errors: 它们会产生以下错误:

在此输入图像描述

在此输入图像描述

What's the correct way of doing it? 这样做的正确方法是什么?

Update: 更新:

Code that works 有效的代码

var parcelDict = ["trackingNumber" : parcel.number!,
                  "dstCountry" : parcel.countryCode!];

if (parcel.postalService != nil) {
    parcelDict["postalService"] = parcel.postalService;
}

var jsonError : NSError?
let dict = parcelDict as NSDictionary
var data = NSJSONSerialization.dataWithJSONObject(dict, options:nil, error: &jsonError)
return NSString(data: data!, encoding: NSUTF8StringEncoding)!

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: 否则,在ex方法中使用Swift Dictionary和NSDictionary时,它们的处理方式几乎相同:

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. 如果将Dictionary值类型更改为非可选String,则错误将消失。

[String:String?] change to -> [String:String]

You can use this easy method 您可以使用这种简单的方法

let dictSwift = ["key1": "value1", "key1": value2]

let dictNSMutable = NSMutableDictionary(dictionary: dictSwift)

Enjoy coding! 享受编码!

Don't don't need to convert it to an NSDictionary . 不需要将其转换为NSDictionary Just use: 只需使用:

var data = NSJSONSerialization.dataWithJSONObject(parcelDict, options:0, error: errorPtr) as NSData

It's mentioned in this post: Making a JSON object in Swift 在这篇文章中提到: 在Swift中创建一个JSON对象

Edit 编辑

From your screenshots I believe your problem lies in the value type in your parcelDict Dictionary. 从您的屏幕截图中我相信您的问题在于parcelDict字典中的值类型。 I can recreate the error with the following code: 我可以使用以下代码重新创建错误:

let dict = [String: String?]()
let nsDict = dict as NSDictionary // [String: String?] is not convertible to NSDictionary

However, using a String instead of a String? 但是,使用String而不是String? as the value type removes the error. 因为值类型会删除错误。

Therefore, when populating parcelDict perhaps you should only add to it values which are not nil . 因此,在填充parcelDict您可能只应添加不为nil值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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