ios - 在 iOS 12 中获取数据

标签 ios swift nsurlsession ios12

<分区>

当我尝试使用 Xcode 10 Beta 和 Swift 4.2 获取数据时出现错误。

在 swift 4.1 中,我的方法运行得很完美,现在我遇到了一个错误,我无法在互联网上找到修复它的解决方案。

这是我的代码:

// Download photos from Google for products avatar
func fetchPhotosForProductsFromGoogle(){

    guard let requestURL = NSURL(string: URL.googleAPI + "q=\(query ?? "flagUK")" + URL.searchPhotosOnly + URL.googleSearchEngineID + URL.googleAPIKey)
        else{
            fatalError("URL can't be found.")
    }

    URLSession.shared.dataTask(with: requestURL) { (data, response, error) in

        guard error == nil else {
            print(error?.localizedDescription ?? "Error URL Session.")
            return
        }
        do {

            let googlePhotosList = try JSONDecoder().decode(GoogleSearchModel.self, from: data!)

        } catch {
            print(error.localizedDescription)
        }
        DispatchQueue.main.async {

            self.productsTableView.reloadData()
        }

        }.resume()
}

这是错误的图片:

enter image description here

任何想法都会对我有很大帮助!谢谢。

最佳答案

在 swift 3 之前,swift 类型和它们在 objc 中的计数器类型之间存在隐式桥接,但在 swift 3 之后,它被删除了,在传递这种类型时你必须明确,引用 here了解更多详情

所以你可以这样做

with: requestURL as URL

或者使用 URL 而不是 NSURL

关于ios - 在 iOS 12 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085430/

相关文章:

ios - 我在哪里为 Swift 2 中的 NSURLSession 指定 ReloadIgnoringLocalCacheData

ios - APNS token 冲突,存储在 Postgres 中

ios - 在 UIBarButtonItem 中添加后退箭头

ios - 排序 nsfetchedresultscontroller

swift - 在 Kotlin 中嵌套枚举的最佳方式?

android - 是否可以在后台实现不间断的 API 端点 ping?

ios - 在自定义颜色 Canvas 上绘制 UIImage

ios - ALAssetsGroup numberOfAssets 在使用照片升级的 iPhone 上使用照片库返回 0

swift - 强制展开可选的优点是什么?

ios - swift iOS - NSURLSession 无法正确下载多个文件