[英]Cannot convert return expression of type () to return type WebImage?
internal func fetchConvoImage(for otherUserEmail: String, completion: @escaping (WebImage?) -> ()) {
let path = "images/\(otherUserEmail)_profile_picture.png"
StorageManager.shared.downloadURL(for: path) { result in
switch result {
case .success(let url):
completion(WebImage(url: url))
case .failure(let error):
print("failed to get other user image url: \(error)")
completion(nil)
}
}
}
internal func getConvoImage(for otherUserEmail: String) -> WebImage? {
fetchConvoImage(for: otherUserEmail) { image in
if let image = image {
return image
} else {
return nil
}
}
}
我想使用SDWebImageSwiftUI獲取具有給定 URL 的圖像並將其返回,但是當我調用 fetchConvoImage() 並嘗試使用在閉包中接收到的圖像時,系統告訴我無法將類型 () 的返回表達式轉換為返回類型網絡圖像?
getConvoImage 的閉包在getConvoImage
fetchConvoImage
給StorageManager
的閉包中調用時返回一個值,但您沒有將該返回值傳遞回fetchConvoImage
。 (例如,行completion(WebImage(url: url))
調用completion
但不將其發送回您的 fetch 方法)。
你可以試試這個:
func getConvoImage(for otherUserEmail: String) -> WebImage? {
var result: WebImage?
fetchConvoImage(for: otherUserEmail) { image in
result = image
return image
}
return result
}
這添加了一個result
變量,本地到getConvoImage
,它的值設置在閉包內。 然后,在調用使用閉包的fetchConvoImage
, result
將被設置,並且可以返回。
我也減
if let image = image {
return image
} else {
return nil
}
至
return image
因為在這種情況下if let...
測試是不必要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.