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

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift