0

I'm going to init an array with 6 UIImageViews in this way:

var imageViews = [UIImageView](repeating: UIImageView(), count: 6)

But I found that every element in it has the same address. If I change one imageView I'll change all of them.

I don't know how it works and what should I do to separate them?

Qyy
  • 33
  • 8
  • There are a few ideas posted in this question https://stackoverflow.com/questions/24395105/how-to-create-a-fixed-size-array-of-objects – E. Bogren Nov 23 '18 at 08:57

1 Answers1

0

That's just how it works.

If you check the documentation, it states:

Creates a new collection containing the specified number of a single, repeated value.

If you want separate values, create them explicity

var imageViews = [UIImageView(), UIImageView(), UIImageView(), ...]

Or use a loop. But always check the docs if something unexpected happens because you will either have found a bug in the SDK, or an error in your own assumptions.

dandan78
  • 13,328
  • 13
  • 64
  • 78