2d array of images

Hi,

I have two UIImage arrays, [images1] and [images2} each consisting of 6 jpg images that are in my project. I want to create an array majorPixArray of these arrays. I keep getting different errors. My swift code an explanations are below.


import UIKit

class StoryViewController: UIViewController {

let images1 : [UIImage] = [

UIImage(named: "bathing1.jpg")!, UIImage(named: "bathing2.jpg")!, UIImage(named: "bathing3.jpg")!, UIImage(named: "bathing4.jpg")!, UIImage(named: "bathing5.jpg")!, UIImage(named: "bathing6.jpg")! ]

let images2 : [UIImage] = [

UIImage(named: "goodssport1.jpg")!, UIImage(named: "goodssport2.jpg")!, UIImage(named: "goodssport3.jpg")!, UIImage(named: "goodssport4.jpg")!, UIImage(named: "goodssport5.jpg")!, UIImage(named: "goodssport6.jpg")! ]

*** let majorPixArray : [[UIImage]] = [[images1], [images2]]

override func viewDidLoad() {

etc., etc.

The *** line throws an error: "Cannot use instance member 'images1' within property initializer; property initializers run before 'self' is available"

If I try to put the *** line in as a function later in the code after view did load, like this:

func putInPix() {

let majorPixArray : [[UIImage]] = [[images1], [images2]]

}

i get the error there as : Cannot convert value of type '[UIImage]' to expected element type 'UIImage'

So I guess what I'm asking is how to define and initialize an array of UIImage arrays? Thanks.

As the first error told you, you can't create the 3rd array until after the instance has been initialized. But you do want "majorPixArray" to be a property of the class, not a local variable as in your attempt to fix the problem. So, you need to break it down into separate declaration and initialization parts:


     let images1: [UIImage] = …
     let images2: [UIImage] = …
     var majorPixArray: [[UIImage]]! = nil
     …
     override func viewDidLoad () {
          majorPixArray = … // see comments below
          …
     }


But if you take that approach, the "majorPixArray" property has to be some kind of optional, because it's nil until set, and it has to a "var" property rather than a "let" property. To fix that, you need to set it in an initializer instead of viewDidLoad:


     let images1: [UIImage] = …
     let images2: [UIImage] = …
     let majorPixArray: [[UIImage]]
     override init (nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
          super.init (nibName: nibNameOrNil, bundle: nibNameOrNil)
          majorPixArray = … // see comments below
     }
     required init? (coder: NSCoder) {
          super.init (coder: coder)
     }


That's what you wanted, but notice that because you overrode one superclass initializer, you had to override both.


Finally, note that "images1" and "images2" are already arrays, so you do not enclose them in braces when you reference them, which is why you got that last error. The correct way to set the "majorPixArray" value is:


     majorPixArray = [images1, images2]
2d array of images
 
 
Q