How to store immutable arrays in a variable stored property in Swift?

jemmons

I would like my class to have a stored property that can be assigned immutable arrays. If I do this:

class MyClass{
  var myItems:[String]
}

I can assign different arrays to my property, but the arrays will be mutable. If I do this:

class MyClass{
  let myItems:[String]
}

My array is immutable, but I can't ever change what's assigned to it. Is there any way to have my cake not mutate it too?

The best I've come up with is creating a wrapper around the array, then using that type for the property, like so:

class MyClass{
  struct ImmutableWrapper{
    let array:[String]
  }
  var myItems:ImmutableWrapper
}

…which is not exactly elegant.

matt

The question is oddly posed, since your first myItems is not an array of various arrays, it's just an array of Strings. However, I'll try to discuss the matter in a more general way.

Swift does not distinguish mutable from immutable arrays in the way Cocoa NSArray does. But why do you need this? Cocoa needs it because an NSArray is a class, so if it were mutable, it could be changed behind MyClass's back. But in Swift, Array is a value type, so that can't happen.

So what's the problem you are really trying to solve? You should ask yourself what contract you are trying to get outside objects to fulfill, and try to write that contract into the structure of MyClass. Your array of structs is a perfectly reasonable approach. I think, though, that what you're after here might actually be privacy, not immutability. For example, I might say this:

class MyClass{
    private var myItemsHidden = [String]()
    var myItems:[String] {
        get {
            return myItemsHidden
        }
        set {
            myItemsHidden = newValue
        }
    }
}

Now, assuming MyClass is alone in its own file (because private in Swift means private to a file), myItemsHidden can be manipulated in any way you like from inside MyClass, so presumably MyClass knows not to mutate any subarrays or whatever it is you are trying to prevent. It is up to MyClass what it allows itself to do with myItemsHidden.

But from outside MyClass, myItemsHidden does not exist; there is only myItems, and the only thing anyone can do to it is get it and set it. (If they get it and change it, the original is unaffected. They cannot mutate the original.) If your purpose is to prevent anyone from outside setting myItems at all, delete the setter function:

class MyClass{
    private var myItemsHidden = [String]()
    var myItems:[String] {
        get {
            return myItemsHidden
        }
    }
}

Now myItems is merely a dispensed (vended) array and no more.

If the idea is that other classes should be allowed to add new arrays to an array of arrays, you could add a MyClass method that lets them do that. In other words, now that this thing is private, it is up to you what kind of access you give others to it. MyClass becomes the gatekeeper for what other classes can do with this value, because the value itself is hidden behind the private wall.

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 to change a variable stored property in a structure in Swift?

From Dev

How To Store Swift function in a variable

From Dev

How To Store Swift function in a variable

From Dev

How do I initialize an immutable Swift property to a computed value ?

From Dev

How to get the arrays in a variable which can be stored as different arrays?

From Dev

Swift optional Array property is immutable?

From Dev

Swift optional Array property is immutable?

From Dev

How can I store huge arrays in swift

From Dev

How to store values from an observable in a a variable or property

From Java

Overriding a stored property in Swift

From Dev

How to store result of stored procedure in a variable using SQL Server

From Dev

How to store query result in a variable in MySql stored Procedure

From Dev

How to store this objectiveC block inside a swift variable?

From Dev

How to store a variable in Swift - login system?

From Dev

How to store a variable in Swift - login system?

From Dev

How do I create a weak stored property in a Swift extension?

From Dev

Initialize property of type immutable dictionary in Swift init()

From Dev

How to call in another .swift file a variable property?

From Dev

How are arrays stored on the stack?

From Dev

How are arrays stored on the stack?

From Dev

How to fix Swift 3 error ‘cannot use mutating member on immutable value: ‘loadedPost’ get only property’?

From Java

Store a closure as a variable in Swift

From Dev

Store Swift Type in variable

From Dev

Store a reference to a property of an object in a variable

From Dev

Store a reference to a property of an object in a variable

From Dev

swift closure stored and access as a variable

From Dev

Swift "is" operator with type stored in variable

From Java

How to store arrays in MySQL?

From Dev

How to store the decimal value of a Float Type in a variable (Swift)

Related Related

  1. 1

    How to change a variable stored property in a structure in Swift?

  2. 2

    How To Store Swift function in a variable

  3. 3

    How To Store Swift function in a variable

  4. 4

    How do I initialize an immutable Swift property to a computed value ?

  5. 5

    How to get the arrays in a variable which can be stored as different arrays?

  6. 6

    Swift optional Array property is immutable?

  7. 7

    Swift optional Array property is immutable?

  8. 8

    How can I store huge arrays in swift

  9. 9

    How to store values from an observable in a a variable or property

  10. 10

    Overriding a stored property in Swift

  11. 11

    How to store result of stored procedure in a variable using SQL Server

  12. 12

    How to store query result in a variable in MySql stored Procedure

  13. 13

    How to store this objectiveC block inside a swift variable?

  14. 14

    How to store a variable in Swift - login system?

  15. 15

    How to store a variable in Swift - login system?

  16. 16

    How do I create a weak stored property in a Swift extension?

  17. 17

    Initialize property of type immutable dictionary in Swift init()

  18. 18

    How to call in another .swift file a variable property?

  19. 19

    How are arrays stored on the stack?

  20. 20

    How are arrays stored on the stack?

  21. 21

    How to fix Swift 3 error ‘cannot use mutating member on immutable value: ‘loadedPost’ get only property’?

  22. 22

    Store a closure as a variable in Swift

  23. 23

    Store Swift Type in variable

  24. 24

    Store a reference to a property of an object in a variable

  25. 25

    Store a reference to a property of an object in a variable

  26. 26

    swift closure stored and access as a variable

  27. 27

    Swift "is" operator with type stored in variable

  28. 28

    How to store arrays in MySQL?

  29. 29

    How to store the decimal value of a Float Type in a variable (Swift)

HotTag

Archive