gpt4 book ai didi

ios - 对成员 'datatask(with:completionhandler:)' 的模糊引用

转载 作者:行者123 更新时间:2023-11-28 08:18:58 24 4
gpt4 key购买 nike

我的代码有问题。我正在尝试通过 StringBuilder 下载图片,然后将其设置为 UIImage 我似乎遇到了问题,我希望有人能看到我做错了什么。

设置界面:

uiMovieTitle.text = self.movies![movieIndex].title

var finalImageUrl = StringBuilder()

let session = URLSession(configuration: .default)

let downloadPicTask = session.dataTask(with: finalImageUrl) { (data, response, error) in
// The download has finished.
if let e = error {
print("Error downloading cat picture: \(e)")
} else {
// No errors found.
// It would be weird if we didn't have a response, so check for that too.
if let res = response as? HTTPURLResponse {
print("Downloaded cat picture with response code \(res.statusCode)")
if let imageData = data {
// Finally convert that Data into an image and do what you wish with it.
let image = UIImage(data: imageData)
// Do something with your image.
uiMoviePoster.image = image
} else {
print("Couldn't get image: Image is nil")
}
} else {
print("Couldn't get response code for some reason")
}
}
}

downloadPicTask.resume()

}

字符串生成器

func StringBuilder() -> (String){
let posterBase = "http://image.tmdb.org/t/p/w1920"
let linkEnd = self.movies?[movieIndex].posterPath

var finalLink = ""

finalLink = posterBase + linkEnd!

return finalLink

}

我还下载了另一个电影列表 (JSON),这对 StringBuilder 至关重要。

最佳答案

编译器报错是因为函数 SwiftBuilder 返回一个 String 并且 URLSession 上有多个名为 dataTask(with: completion:),但它们都没有将 String 作为第一个参数。

如果您需要 SwiftBuilder 继续为代码的其他部分返回字符串,那么在这里您需要将该字符串转换为 URL。

像下面这样的东西应该可以工作:

let session = URLSession(configuration: .default)

let imageUrlString = StringBuilder()
if let imageUrl = URL(string: imageUrlString) {
let downloadPicTask = session.dataTask(with: imageUrl) { (data, response, error) in
// The download has finished.

等等...让我知道这是否有意义。

关于ios - 对成员 'datatask(with:completionhandler:)' 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41780591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com