Ios 在swift中从API调用传递json数组的所有成员

Ios 在swift中从API调用传递json数组的所有成员,ios,swift,alamofire,sdwebimage,Ios,Swift,Alamofire,Sdwebimage,你好,我有一个项目,我实现了ImageSlideshow和sdwebimage。图像是通过API调用获得的,但在ImageSlideshow的文档中,SDwebImages的实现如下 let sdWebImageSource = [SDWebImageSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080")!, SDWebImageSource(urlString: "http

你好,我有一个项目,我实现了ImageSlideshow和sdwebimage。图像是通过API调用获得的,但在ImageSlideshow的文档中,SDwebImages的实现如下

let sdWebImageSource = [SDWebImageSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080")!, SDWebImageSource(urlString: "https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080")!, SDWebImageSource(urlString: "https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080")!]
这很好,但对于我自己的项目,我有一个数组中的所有图像,我想在ImageSlideshow中显示这些图像。我不知道图像的数量,因此很难对其进行硬编码,因此如何传递图像数组
ProductServices.instance.selectedProduct?.productImg[0])
这提供了第一个索引处的图像。图像可能达到第五个索引。我怎样才能通过考试?

你可以试试

var allImages = [SDWebImageSource]()
if let allStr = ProductServices.instance.selectedProduct?.productImg {
    for str in allStr {
       allImages.append(SDWebImageSource(urlString:str))  
   }
}

if allImages.isEmpty {
   // display custom image // or add it's default str 
}

productImg[0]是图像url字符串,您想像上面的示例一样填充数组吗?是,使用
productImg[0]
productImg[1]
productImg[2]
。。。。我不知道数组中可能包含的数字,如果它是空的,我想显示一个自定义图像
if allImages.isEmpty{//display custom image//或添加它的默认str slideshow.setImageInputs(localImg)}else{slideshow.setImageInputs(allImages)}
当我单击带有空图像的单元格时,应用程序崩溃了`let localImg=[ImageSource(imageString:“image\u not\u found”)!]`localImg是崩溃的原因,因为您为默认图像提供了无效的url,我不知道你的UI,但你可以放置默认图像,并根据是否有图像来管理其可见性,或者为默认图像提供一个有效的url。我遇到了问题。那是因为我强行打开了它。多谢各位