objective-c - swift http请求demo报错“Error Domain=NSCocoaErrorDomain Code=3840 "
習慣沉默
習慣沉默 2017-04-28 09:06:31
0
0
676

func loadDataSource() {

    self.refreshControl!.beginRefreshing()
    var loadURL = NSURL(string: hackerNewsApiUrl)
    var request = NSURLRequest(URL: loadURL!)

// var error: NSError? = nil

    var loadDataSourceQueue = NSOperationQueue();
    NSURLConnection.sendAsynchronousRequest(request, queue: loadDataSourceQueue, completionHandler: { response, data, error in
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            let newsDataSource = json["item"] as! NSArray
            var currentNewsDataSource = NSMutableArray()
            for currentNews : AnyObject in newsDataSource {
                let newsItem = XHNewsItem()
                newsItem.newsTitle = currentNews["title"] as! NSString
                newsItem.newsThumb = currentNews["thumb"]as! NSString
                newsItem.newsID = currentNews["id"] as! NSString
                currentNewsDataSource.addObject(newsItem)
                print( newsItem.newsTitle)
            }
            dispatch_async(dispatch_get_main_queue(), {
                self.dataSource = currentNewsDataSource
                self.tableView.reloadData()
                self.refreshControl!.endRefreshing()
            })
        }catch let error as NSError {
            print(error)
            dispatch_async(dispatch_get_main_queue(), {
                self.refreshControl!.endRefreshing()
            })

        }
    })
}
習慣沉默
習慣沉默

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板