【发布时间】:2026-01-17 13:20:06
【问题描述】:
我正在使用 Flickr api 根据使用以下代码的搜索检索图像。该函数将图像 id 发送到端点并返回一个对象,我解析该对象以获得所述图像的特定 url。我得到了我想要的响应,但我正在尝试获取 item.source 中的所有图像 url 并将它们添加到数组中。我尝试返回一个数组,但编译器抱怨这无法完成,因为它是 non-void return from a void method,尽管我将 [String] 设置为返回值。如何将item.source 列表作为要在getURLImages() 之外使用的数组?或者,我创建了如下所示的 urlsList,并尝试将 item.source 附加到此,但这始终打印为空。对于上下文,我计划创建一个UICollectionView 来显示所述图像/网址。
更新
下面是 A 类调用 B 类 getURLImages 的函数:
let result = try JSONDecoder().decode(Response.self, from: data)
if (result.stat == "ok"){
for urls in result.photos!.photo {
DispatchQueue.main.async {
GetImageInfo().getURLImages(photoId: urls.id) { urlsList in
print("Received \(urlsList)")
}
}
}
}
下面是B类的函数:
let result = try JSONDecoder().decode(Response.self, from: data)
var urlList = [String]()
for item in result.sizes!.size {
DispatchQueue.main.async {
if (item.label == "Square"){
urlList.append(item.source)
}
}
}
completion(urlList)
如何在函数范围之外访问 urlsList 属性,以便将其实现到 UICollectionView 中
【问题讨论】: