不能使用string类型的索引下标NSDictionary类型的值

时间:2015-05-22 09:33:36

标签: swift indexing subscript

    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"] 

这是错误发生的地方,我只想从我的jsonResult中取一个值,该值在控制台上正确打印。

1 个答案:

答案 0 :(得分:25)

您将jsonResult类型转换为NSDictionary,而是使用[String:AnyObject]

如果您使用NSDictionary,则应使用valueForKey(key)的{​​{1}}或objectForKey(key)方法获取密钥的值。

NSDictionary