【问题标题】:I keep getting a use unresolved identifier error swift我不断收到使用未解决的标识符错误 swift
【发布时间】:2016-11-25 18:37:27
【问题描述】:

当我尝试运行这个项目时,我遇到了“使用未解决的标识符错误”。这是我得到错误的代码

var jsonDict = 试试 NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) 作为! NSDictionary 让任务:NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 如果((错误)!=零){ 打印(错误!.localizedDescription) } 别的 { 让错误:NSError? 做 { var jsonDict = 试试 NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NS词典 } 抓住 { 如果(错误!=零){ print("JSON 错误 \(err!.localizedDescription)") } 别的 { //5: 提取引号和值并在 NSNotification 中发送它们 让引号:NSArray = ((jsonDict.objectForKey("query") as!NSDictionary).objectForKey("results") as!NSDictionary).objectForKey("quote") as! NSArray dispatch_async(dispatch_get_main_queue(), { NSNotificationCenter.defaultCenter().postNotificationName(kNotificationStocksUpdated, object: nil, userInfo: [kNotificationStocksUpdated:quotes]) }) } } } })

有人可以帮忙吗?谢谢。

【问题讨论】:

  • 你在哪一行得到错误?
  • 是的,它出现在哪一行?
  • @WMios 我刚刚更新了代码以显示错误的位置
  • @LinkOpenheim 我刚刚更新了代码以显示错误的位置
  • 你定义了你的data! NSData 类型吗?

标签: ios swift debugging error-handling


【解决方案1】:

你的问题可能是catch 块中的这行代码。

let quotes:NSArray = ((jsonDict.objectForKey("query") as! NSDictionary).objectForKey("results") as! NSDictionary).objectForKey("quote") as! NSArray

在上述声明中jsonDict 超出范围。您在 do 块中声明了 jsonDict,但试图在 catch 块中使用它。

【讨论】:

    【解决方案2】:

    尝试以下:- (假设JSON有根节点结构)

    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: yourURL)
            let session = NSURLSession.sharedSession()
            let task = session.dataTaskWithRequest(urlRequest) {
                (data, response, error) -> Void in
    
                let httpResponse = response as! NSHTTPURLResponse
                let statusCode = httpResponse.statusCode
    
                if (statusCode == 200) {
    
                    do{
    
                        let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
    
    
    
                        if let queries = json["query"] as? [[String: AnyObject]] {
                                for query in queries {
                                    if let quote = query["quote"] as? String {
                                            self.quoteArr.append(quote)
                                    }
    
                                }//for loop
                                dispatch_async(dispatch_get_main_queue(),{
                                   // your main queue code 
    NSNotificationCenter.defaultCenter().postNotificationName(kNotificationStocksUpdated, object: nil, userInfo: [kNotificationStocksUpdated:quotes])
                                })//dispatch
    
                        }// if loop
    
                    }
                    catch
                    {
                        print("Error with Json: \(error)")
    
                    }
    
                }
                else
                {
                    // No internet connection
                    // Alert view controller
                    // Alert action style default
    
    
                }
    

    【讨论】:

      【解决方案3】:

      这个代码蜘蛛网正是SwiftyJSON 库存在的原因。我强烈推荐它,它可以使用 cocoapods 导入到您的项目中。

      使用这个库生成的代码将是

      jsonQuery["query"]["results"]["quote"]
      

      它更具可读性,并且随着您实现更多 API,速度会更快。

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2017-04-10
        • 2016-07-03
        • 1970-01-01
        相关资源
        最近更新 更多