Search code examples
iosswiftalamofiresdwebimage

passing all members of a json array from an API call in swift


Hi have a project which I implemented ImageSlideshow and sdwebimage. Images are gotten from an API call but in the docs of ImageSlideshow SDwebImages are implemented as below

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")!]

which works well but for my own project I have all the images in an array and I want to display this images in the ImageSlideshow. I do not know the number of images so it is difficult to hard code it so how can I pass the array of images. ProductServices.instance.selectedProduct?.productImg[0]) this gives me the image at the first index. the images could be up to the fifth index. how can I pass this?


Solution

  • You can try

    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 
    }