NSInvalidArgumentException JSON中的无效类型写入DemographicsPojo

时间:2017-02-16 14:02:49

标签: swift alamofire

当我尝试在JSONSerialization代码上使用Alamofire运行以下swift 3时,我收到了 NSInvalidArgumentException

  

试试吧! JSONSerialization.data(withJSONObject:postData,options:JSONSerialization.WritingOptions.prettyPrinted)

在调试时,postData返回以下值。

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反向工程为swift。我想知道我是否需要避免在Pojo中完全使用Optionals?这是问题吗?

1 个答案:

答案 0 :(得分:0)

Swift不会将任意对象序列化为JSON字符串。您可以通过拨打JSONSerialization.isValidJSONObject来查看此内容。

您需要先将对象转换为字典。或者使用第三方JSON序列化程序库,例如Gloss

相关问题