Convert the Dictionary to JSON iOS swift

Solution:

The below function was used to convert the dictionary to jsonobject.

You can call the function like below.



convertToJSON(dict: dictionary)

Code:


func convertToJSON(dict: NSDictionary) -> String{
        if let theJSONData = try? JSONSerialization.data(
            withJSONObject: dict,
            options: []) {
            let theJSONText = String(data: theJSONData,
                                     encoding: .ascii)
            print("JSON string = \(theJSONText!)")
            return theJSONText!
        }
        return ""
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift