如帖子标题中所述,我在尝试快速将 Dictionary 转换为 JSON Data 时收到 NSInvalidArgumentException - 'Invalid top-level type in JSON write'
let userInfo: [String: String] = [
"user_name" : username!,
"password" : password!,
"device_id" : DEVICE_ID!,
"os_version" : OS_VERSION
]
let inputData = jsonEncode(object: userInfo)
. . .
static private func jsonEncode(object:Any?) -> Data?
{
do{
if let encoded = try JSONSerialization.data(withJSONObject: object, options:[]) as Data? <- here occured NSInvalidArgumentException
if(encoded != nil)
{
return encoded
}
else
{
return nil
}
}
catch
{
return nil
}
}
我将字典作为参数传递,而不是出错。请帮帮我。
谢谢!