ios 如何将 swift Dictionary 转换为 NSDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29502878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:33:28  来源:igfitidea点击:

How do I convert swift Dictionary to NSDictionary

iosswiftnsdictionary

提问by BergP

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](a 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 = parcelDictdoes 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:

所有这些示例都不起作用。它们产生以下错误:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

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)!

回答by Arbitur

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:

否则,在方法中使用 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?] change to -> [String:String]

回答by Gopal Devra

You can use this easy method

你可以使用这个简单的方法

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

let dictNSMutable = NSMutableDictionary(dictionary: dictSwift)

Enjoy coding!

享受编码!

回答by ABakerSmith

Don't don't need to convert it to an NSDictionary. Just use:

不需要将其转换为NSDictionary. 只需使用:

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 parcelDictDictionary. I can recreate the error with the following code:

从你的截图我相信你的问题在于你的parcelDict字典中的值类型。我可以使用以下代码重新创建错误:

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

However, using a Stringinstead of a String?as the value type removes the error.

但是,使用 aString而不是 aString?作为值类型可以消除错误。

Therefore, when populating parcelDictperhaps you should only add to it values which are not nil.

因此,在填充时,parcelDict您可能应该只向它添加不是nil.