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

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift