How can I make an immutable Array in swift that has different instances of the same class

o.uinn

So in swift there isn't a distinct difference between an Array, and a mutable Array, but the Apple docs say it is always better to use an immutable Array when you know the number of elements. So say I want to make an immutable Array of type Hi that has x different elements of type Hi.

I can do this but it will assign the same instance to each element in the array

var array = [Hi](count: x, repeatedValue: Hi())

I can do this but the elements in the array are let, and therefore not editable

var array = [Hi](count: x, repeatedValue: nil)
for hi in array {
hi = Hi()
}

Is there a constructor for Array that takes a closure so I can tell it how to make each element?

GoZoner

As an expression, simply:

[Int](count: x, repeatedValue: 0).map { _ in Hi() }

or, more succinctly, as suggested by @vacawama and using Range.map

(1...x).map { _ in Hi() }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make objects of the same class do different things in the same function?

From Dev

hashCode can be the same for different instances of an inner class?

From Dev

how do I create an array populated by class instances in swift?

From Dev

In Swift how can I make different implementations of a class depending on whether it is declared var or let?

From Dev

In Swift how can I make different implementations of a class depending on whether it is declared var or let?

From Dev

How can I create instances of a ruby class from a hash array?

From Dev

How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

From Dev

PHP, after setting an array in a method, how can I get it from a different method in same class

From Dev

How can I make the same function work for different arrays?

From Dev

how can I fade in and out different texts on the same UILabel in Swift?

From Dev

How can I declare same object name for different class?

From Dev

How can I run two different main class at the same time?

From Dev

can moderm JVMs optimize different instances of the same class differently?

From Dev

Combine different instances of the same class

From Dev

How can I tell if the elements in an Array List are same or different?

From Dev

How can I make multiple instances of the same panel and change attributes in each one individually?

From Dev

How to make class immutable in python?

From Dev

How to use 2 different instances of the same class intest?

From Dev

How can I make a extension for array of specific type in Swift

From Dev

How can I make the CGFloat array for setLineDash in Swift

From Dev

How can I make the Array List be called in another class java

From Dev

How to check two instances are the same class/type in swift

From Dev

How can I have different data in different ReactJS Component instances?

From Dev

How to implement a class that has multuple instances, none of which can be equal

From Dev

how can I declare an immutable class in C++ (Java Example)

From Dev

How can I have two different endpoint with different namespace and same JAXB class?

From Dev

How can I make it so the user can input different answers but display the same result?

From Dev

How can I make it so the user can input different answers but display the same result?

From Dev

How do I reference all class instances at the same time?

Related Related

  1. 1

    How can I make objects of the same class do different things in the same function?

  2. 2

    hashCode can be the same for different instances of an inner class?

  3. 3

    how do I create an array populated by class instances in swift?

  4. 4

    In Swift how can I make different implementations of a class depending on whether it is declared var or let?

  5. 5

    In Swift how can I make different implementations of a class depending on whether it is declared var or let?

  6. 6

    How can I create instances of a ruby class from a hash array?

  7. 7

    How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

  8. 8

    PHP, after setting an array in a method, how can I get it from a different method in same class

  9. 9

    How can I make the same function work for different arrays?

  10. 10

    how can I fade in and out different texts on the same UILabel in Swift?

  11. 11

    How can I declare same object name for different class?

  12. 12

    How can I run two different main class at the same time?

  13. 13

    can moderm JVMs optimize different instances of the same class differently?

  14. 14

    Combine different instances of the same class

  15. 15

    How can I tell if the elements in an Array List are same or different?

  16. 16

    How can I make multiple instances of the same panel and change attributes in each one individually?

  17. 17

    How to make class immutable in python?

  18. 18

    How to use 2 different instances of the same class intest?

  19. 19

    How can I make a extension for array of specific type in Swift

  20. 20

    How can I make the CGFloat array for setLineDash in Swift

  21. 21

    How can I make the Array List be called in another class java

  22. 22

    How to check two instances are the same class/type in swift

  23. 23

    How can I have different data in different ReactJS Component instances?

  24. 24

    How to implement a class that has multuple instances, none of which can be equal

  25. 25

    how can I declare an immutable class in C++ (Java Example)

  26. 26

    How can I have two different endpoint with different namespace and same JAXB class?

  27. 27

    How can I make it so the user can input different answers but display the same result?

  28. 28

    How can I make it so the user can input different answers but display the same result?

  29. 29

    How do I reference all class instances at the same time?

HotTag

Archive