lightbox showing array of images

heyy coders i am stucking in this problem

            var val1 : String? = ""
            var val2 : String? = ""
            var val3 : String? = ""
            var val4 : String? = ""
            
            let img = phoneImage[indexPath.row].imageUrl.enumerated().forEach {  index,string in
                if index == 0 {
                    val1?.append(string)
                    print(string)
                } else if index == 1 {
                    val2?.append(string)
                } else if index == 2 {
                    val3?.append(string)
                }else if index == 3 {
                    val4?.append(string)
                }
            }
              
            let imagess = [LightboxImage(imageURL: URL(string: val1!)!),
                           LightboxImage(imageURL: URL(string: val2!)!),
                           LightboxImage(imageURL: URL(string: val3!)!),
                           LightboxImage(imageURL: URL(string: val4!)!) ]

let me explain what is problem i need some idea to append other array downbelow code

let imagess = [LightboxImage(imageURL: URL(string:val1!)!),
 LightboxImage(imageURL: URL(string:val2!)!),
 LightboxImage(imageURL: URL(string:val3!)!),
 LightboxImage(imageURL: URL(string:val4!)!) ]

this is my final array every calling time is need update but everytime comeing this type LightboxImage(imageURL: URL(string: val1!)!) how to i figure it how i append LightboxImage(imageURL: URL(string: val1!)!) to array every time if 3. and 4. value nil app is crash it Thank you for help.

Accepted Answer

Hello  ayhanhakan

Your problem is on the first "if", you had a print out the string, and no one other "ifs" had this print. So, trying to add print to "else if" as well.

Good luck!

Cleber.

lightbox showing array of images
 
 
Q