unwrapping constructors swift indexing subscript

swift - constructors - no puede subindicar un valor de tipo NSDictionary con un índice de tipo cadena



swift super (1)

var url: NSURL = NSURL(string: urlPath)! var request: NSMutableURLRequest = NSMutableURLRequest(URL: url) request.setValue("Basic /(base64EncodedCredential)", forHTTPHeaderField: "Authorization") request.HTTPMethod = "GET" var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error:nil)! var err: NSError println(dataVal) //var jsonResult : NSDictionary? var jsonResult = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary println("Synchronous /(jsonResult)") jsonResult["records"]

Aquí es donde ocurre el error, solo quiero tomar un valor de mi jsonResult , que se imprime correctamente en la consola.


Está encasillando el jsonResult como NSDictionary , en su lugar use [String:AnyObject]

Si está utilizando NSDictionary , debe usar los valueForKey(key) u objectForKey(key) de NSDictionary para obtener el valor de la clave.

var url: NSURL = NSURL(string: urlPath)! var err: NSError? var request: NSMutableURLRequest = NSMutableURLRequest(URL: url) request.setValue("Basic /(base64EncodedCredential)", forHTTPHeaderField: "Authorization") request.HTTPMethod = "GET" var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error:nil)! var err: NSError println(dataVal) //var jsonResult : NSDictionary? var jsonResult:AnyObject? = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableLeaves, error: &err) println("Synchronous /(jsonResult)") if let result = jsonResult as? [String: AnyObject] { if let oneValue = result["records"] as? String { //Here i am considering value for jsonResult["records"] as String, if it other than String, please change it. println(oneValue) } }