0

My swift code below is trying to initizilie a array of UIimages. Right now the code is causing a compile error. I have also added a photo of where my images are store in Xcode. The code is below. enter image description here

let image1 = UIImage(named: "a.png")
let image2 = UIImage(named: "b.png")
let image3 = UIImage(named: "c.png")
let image4 = UIImage(named: "d.png")
let image5 = UIImage(named: "e.png")
let image6 = UIImage(named: "f.png")
lazy var images: [UIImage?] = { [image1, image2, image3, image4, image5, image6] }()
1
  • 1
    Why didn't you add the images in Assets? Commented Apr 3, 2020 at 5:44

3 Answers 3

1

Refer the bellow code

let images = [image1, image2, image3, image4, image5, image6]

when you are creating new empty array, then only the type need to mentioned and the open close parenthesis is also required

To declare empty array of images

let images = [UIImage]()

Your code can also rewrite like bellow, instead of creating these much variables unnecessarily

let images: [UIImage?] = [
    UIImage(named: "a.png"),
    UIImage(named: "b.png"),
    UIImage(named: "c.png"),
    UIImage(named: "d.png"),
    UIImage(named: "e.png"),
    UIImage(named: "f.png")
]
Sign up to request clarification or add additional context in comments.

Comments

0

You only need the following line to create an image array

let images = [image1, image2, image3, image4, image5, image6]

Comments

0

Remove the curly bracket and parentheses.

lazy var images: [UIImage?] =  [image1, image2, image3, image4, image5, image6]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.