How to convert NSObject class object into JSON in Swift?

前端 未结 3 1485
心在旅途
心在旅途 2021-01-19 15:54

I have

var  contacts : [ContactsModel] = []

and

class ContactsModel: NSObject
{
   var contactEmail : String?
   var cont         


        
3条回答
  •  天涯浪人
    2021-01-19 16:37

    You can use NSJSONSerialization for array when array contains only JSON encodable values (string, number, dictionary, array, nil)

    first you need to create the JSON object then you can use it (your code is crashing because contact is not a JSON object!)

    you can refere below link

    https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/doc/uid/TP40010946-CH1-SW9

提交回复
热议问题