NSInvalidArgumentException JSON中的无效类型写入DemographicsPojo [英] NSInvalidArgumentException Invalid type in JSON write DemographicsPojo
问题描述
当我尝试使用 Alamofire
JSONSerialization 时,我得到了 NSInvalidArgumentException >在 swift 3
代码上。
I am getting a NSInvalidArgumentException when I try to run the following JSONSerialization
using Alamofire
on swift 3
code.
尝试! JSONSerialization.data(withJSONObject:postData,选项:JSONSerialization.WritingOptions.prettyPrinted)
try! JSONSerialization.data(withJSONObject: postData, options: JSONSerialization.WritingOptions.prettyPrinted)
在调试时, postData
返回下面的值。
On debug, postData
is returning the value below.
key __NSCFString * "demographicsPojo" 0x000060800005e8a0
value _TtC7MyApp16DemographicsPojo * 0x6080001c5cd0 0x00006080001c5cd0
[0] _TtC7MyApp16DemographicsPojo
createdTimeMilliSecs Int64? 1487253369999
dateOfBirthMilliSecs Int64? 1487253369999
demographicsRoleType Int? 3
gender Int? 0
isDemographicsActive Int? 3
isRecordActive Int? 0
isTermsAndConditionsAccepted Int? 3
licenseExpiryDate Int64? 1487253369999
mobilePhone String? "9876543210" some
termsAndConditionsAcceptedTime Int64? 1487253369999
termsAndConditionsText String? "" some
updatedTimeMilliSecs Int64? 1487253369999
我是 Swift 3
的新手。我使用基于Android的JSON文件对 DemographicsPojo
进行反向工程以快速进行。我想知道是否需要避免在Pojo's中完全使用 Optionals
?这是问题吗?
I am new to Swift 3
. I used my Android based JSON files to reverse engineer the DemographicsPojo
to swift. I wonder if I need to avoid using Optionals
completely in the Pojo's ? Is that the issue ?
推荐答案
Swift不会将任意对象序列化为JSON字符串。您可以通过调用 JSONSerialization.isValidJSONObject
进行检查。
Swift would not serialize arbitrary object into JSON string. You can check this with a call to JSONSerialization.isValidJSONObject
.
您需要先将对象转换成字典。或使用第三方JSON序列化程序库,例如光泽
You need to convert your object into a dictionary first. Or use third-party JSON serializer library, like Gloss
这篇关于NSInvalidArgumentException JSON中的无效类型写入DemographicsPojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!