user6159419
user6159419

Reputation: 247

Swift: Could not cast value of type 'Could not cast value of type '__NSCFString' (0x10e4d5ef0) to 'NSDictionary' (0x10e4d6bc0).'

I am trying to get post data in dictionary. Let first me show what I am doing.

manager.POST(url, parameters: dict,
                success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
    if let jsonDict = self.parseJSON(responseObject) {
    print(jsonDict)
                        
  // Error occur on the following line                       
 
  let dataDict = jsonDict["data"] as! [String : AnyObject]
 
 // Error Info: Swift: Could not cast value of type 'Could not cast value of type '__NSCFString' (0x10e4d5ef0) to 'NSDictionary' (0x10e4d6bc0).'

}
ParseJson Function:
func parseJSON(inputData: AnyObject) -> Dictionary<String,AnyObject>?{
        
        do {
            let jsonDict = try NSJSONSerialization.JSONObjectWithData(inputData as! NSData, options:NSJSONReadingOptions.MutableContainers) as! Dictionary<String,AnyObject>
            // use jsonData
            return jsonDict
        } catch let error as NSError {
            // report error
            print(error.description)
            print("Session: Json Parse Error!!!")
            return nil
        }
    }

This is the result of pirnt(jsonDict)

[data: {"session_id":"ab4kn9fj34ko89kjkl5ljs98gfk498fgkl409alk34l","user_info":{"username":"Johny Cage","ENCPASSWORD":"johnycage","id":1,"FULLNAME":"Johny Cage"}}, status: Success]

Any help or suggestion is highly appreciated.

Upvotes: 1

Views: 1365

Answers (1)

Musaab Zeeshan
Musaab Zeeshan

Reputation: 120

Check your return data from web-service. I think you are converting it in Jason String multiple times, Or something like this. There is no issue in above code. Check your server side.

Upvotes: 1

Related Questions