NSInvalidArgumentException Invalid type in JSON write DemographicsPojo

不想你离开。 提交于 2019-12-11 05:52:06

问题


I am getting a NSInvalidArgumentException when I try to run the following JSONSerialization using Alamofire on swift 3 code.

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

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

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 ?


回答1:


Swift would not serialize arbitrary object into JSON string. You can check this with a call to JSONSerialization.isValidJSONObject.

You need to convert your object into a dictionary first. Or use third-party JSON serializer library, like Gloss



来源:https://stackoverflow.com/questions/42276134/nsinvalidargumentexception-invalid-type-in-json-write-demographicspojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!