尝试将图像 URL 作为字符串获取时应用程序崩溃

App crashing when trying to get image URL as a String

我有这个功能可以请求 API:

Alamofire.request(URL).responseObject{ (response: DataResponse<Movie>) in
        print("|MovieController| Response is: \(response)")

        let movie = response.result.value

        if let posterURL = movie?.posterURL {
            print("Poster URL: \(posterURL)")

            let imgStg: String = posterURL
            print("-------> Image String: \(imgStg)")


        }
}

我确实在我的控制台上正确打印了 posterURL,但是当我尝试让 imgStg: String = posterURL 时应用程序崩溃了。

首先我尝试这样做:

let imgStg: String = movie!.posterURL!
let imgURL: URL? = URL(string: imgStg) //Error here: Cannot call value of non-function type 'String'
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)

但 xcode 显示此错误。所以我试着看看我在 'imgStg' 中得到了什么,并且在执行此行时应用程序崩溃了。这是为什么?我怎样才能做到这一点?

PS.: 我的最终目标是像这样使用 KingFisher 缓存图像:

posterImageView.kf.setImage(with: imgSrc)

错误:


更新

错误:


更新 2:尝试从头开始输入,这就是选项:

您的图像实际上显示了一个断点(而不是您想象的运行时错误)。

要禁用它,只需单击行 左边距。要禁用 all 断点,请改用 command + Y

断点breakpoint 只是给定源代码行上的 强制停止 。因此,它与特定的构建时或运行时错误完全无关。

错误提示您正在调用 String 而不是方法。 如我所见,您正在传递 URL (不建议使用相同的名称)作为请求中的参数,因此它会变得混乱并引发错误。尝试将名称从 URL 更改为 requestURL 或其他名称。

替换为:

Alamofire.request(URL).responseObject { (response: DataResponse<Movie>)

与:

Alamofire.request(requestURL).responseObject { (response: DataResponse<Movie>)

而且肯定是你声明 URL 的地方也用 requestURL 改变了那个地方。