Swift: Creating an array of UIImage

89,866

Solution 1

You have two problems (and without a regex!)

1. You aren't creating an array. You need to do:

var logoImages: [UIImage] = []

or

var logoImages: Array<UIImage> = []

or

var logoImages = [UIImage]()

or

var logoImages = Array<UIImage>()

2. If you want to add new objects to an array, you should use Array.append() or some of the equivalent syntactic sugar:

logoImages.append(UIImage(named: "logo.png")!)

or

logoImages += [UIImage(named: "logo.png")!]

or

logoImages += [UIImage(named: "logo.png")!, UIImage(named: "logo2.png")!]

You need to append to the array because (excerpt from docs):

You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error. However, you can check that an index is valid before using it, by comparing it to the array’s count property. Except when count is 0 (meaning the array is empty), the largest valid index in an array will always be count - 1, because arrays are indexed from zero.

Of course you could always simplify it when possible:

var logoImage: [UIImage] = [
    UIImage(named: "logo1.png")!,
    UIImage(named: "logo2.png")!
]

edit: Note that UIImage now has a "failable" initializer, meaning that it returns an optional. I've updated all the bits of code to reflect this change as well as changes to the array syntax.

Solution 2

You are declaring the type for logoImages but not creating an instance of that type.

Use var logoImages = UIImage[]() which will create a new array for you.

...and then after creating a new empty Array instance, as described in the answer by @Jiaaro you can't use subscripting to add to an empty array

Solution 3

var image : UIImage = UIImage(named:"logo.png")    
var logoImages = [image]
Share:
89,866
kmiklas
Author by

kmiklas

Updated on February 02, 2021

Comments

  • kmiklas
    kmiklas over 3 years

    Using Swift, I'm trying to create an array of UIImage objects for a simple animation. Contextual help for animationImages reads, "The array must contain UI Image objects."

    I've tried to create said array as follows, but can't seem to get the syntax correct:

    var logoImages: UIImage[]
    logoImages[0] = UIImage(name: "logo.png")
    

    This throws: ! Variable logoImages used before being initialized

    Then I tried

    var logoImages = []
    logoImages[0] = UIImage(named: "logo.png")
    

    Which throws: ! Cannot assign to the result of this expression

    I've checked the docs here, but the context isn't the same: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

  • kmiklas
    kmiklas almost 10 years
    First, this throws an error, "Constructive statements on a line must be separated by ';'. Xcode wants `UIImage[];()
  • kmiklas
    kmiklas almost 10 years
    Second, I saw this, but those two parenthesis () scared me off as a function invocation.
  • kmiklas
    kmiklas almost 10 years
    So, as commented on ackStOverflow's response, UIImage[]() throws a Constructive statements on a line must be separated by a ';' error. Also, those () parenthesis look a lot like a function call. Also this looks a little weird, eh? UIImage[];(). Finally, why do I have to use append; why can't I just assign a value to an index?
  • Jiaaro
    Jiaaro almost 10 years
    @kmiklas Array<UIImage> is a Type, you need to instantiate it (using parens): Array<UIImage>(), alternatively if you specify the type, you can just assign an array literal (which is the first two examples). UIImage[] is just syntactic sugar for Array<UIImage>
  • David Berry
    David Berry almost 10 years
    @kmiklas Sounds like you've got something else going on then. The line given by ackSt is correct and shouldn't generate errors for you. Putting a semicolon in where you are is completely changing the meaning.
  • Jiaaro
    Jiaaro almost 10 years
    @kmiklas added excerpt from docs explaining why you need to append
  • fqdn
    fqdn almost 10 years
    @kmiklas make sure you are using var logoImages = UIImage[]() which creates a new Array for storing UIImage objects and then assigns it to the logoImages variable (inferring the variable's type from the assignment)... it sounds like you might be incorrectly using a semicolon in place of the equal sign? i.e. var logoImages: UIImage[]() which would generate the type of error you describe in your comment
  • kmiklas
    kmiklas almost 10 years
    Honestly, I have nothing else going on. It's the first line in the function, and the first syntax option by Jiaaro below works: var logoImages:UIImage[] = []
  • fqdn
    fqdn almost 10 years
    ...also, those two parentheses are a function invocation :) they are calling the init() method on Array, in the same way you would do it for any other Structure or Class
  • John Riselvato
    John Riselvato over 9 years
    For some reason the simplified way you have doesn't work any more.
  • Jiaaro
    Jiaaro over 9 years
    @JohnRiselvato UIImage is now a failable initializer (meaning it returns an optional) - will update answer
  • Marcello B.
    Marcello B. over 9 years
    @Jiaaro when you try to run this code it breaks with the error code coder : ObjectiveC.NScoder
  • Supertecnoboff
    Supertecnoboff over 8 years
    Is there no addObject method anymore?