gpt4 book ai didi

swift - 如何将 nil 作为 dictionary 的参数传递

转载 作者:可可西里 更新时间:2023-11-01 02:16:14 26 4
gpt4 key购买 nike

我有一个 Dictionary<String, AnyObject> 类型的字典我需要将参数作为 nil 传递:

let params = [
"number": nil,
"description": nil,
"lang" : "spa",
"byFee": true,
"plan" : data.Plan
] as Dictionary<String, AnyObject>

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])

如果我这样做,我会得到一个错误:

enter image description here

所以我尝试将词典更改为 Dictionary<String, AnyObject?>

但是如果我这样做,那么错误就会出现在请求中。HTTPBody:

enter image description here

最佳答案

JSON 中的空值等同于 ObjC/Swift 中的 NSNull:

let params : [String : AnyObject] = [
"number": NSNull(),
"description": NSNull(),
"lang" : "spa",
"byFee": true,
"plan" : data.Plan,
]

关于swift - 如何将 nil 作为 dictionary<String,AnyObject> 的参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com