1

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 ?

Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • Don't use `try!` carelessly. Use a `do-catch` block to catch a possible error. Set also the exception breakpoint to get detailed information about the exception. – vadian Feb 16 '17 at 14:10

1 Answers1

0

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

paiv
  • 5,491
  • 22
  • 30