Akshay Degada Akshay Degada - 1 year ago 172
Swift Question

How to Convert Response String to NSMutableDictionary in Swift

Response String :
{"status":"failure","message":"link_inactive"} i need same format with NsmutableDictionary or NSDictionary so i'm access via key value, Pleae help any budy i'm new in Swift.

Answer Source

Try it:

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]
            return json
        } catch {
            print("Something went wrong")
    return nil

let string =  "{\"status\":\"failure\",\"message\":\"link_inactive\"}"

let dict = convertStringToDictionary(string) //["status": "failure", "message": "link_inactive"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download