0

Silly beginner Swift question: I am expecting the following 3 lines of code to work in the playground:

let items = ["Apple", "Orange", "Pear"]
items[1] = "Banana" // error here
items

Now the error

error: '@lvalue $T5' is not identical to 'String'
items[1] = "Banana"

My understanding that updating content of immutable array is possible in Swift. I use XCODE 6.1.1

Any idea what is going on here?

Thanks

Based on this thread this was possible in previous releases: Why in immutable array in swift value can be changed?

Community
  • 1
  • 1
Misha Birman
  • 90
  • 1
  • 7
  • Immutable usually means read only, I'm not sure why you would be able to mutate (write to) an immutable array – reggaeguitar Jan 16 '15 at 17:54
  • https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html - "if you assign an array or a dictionary to a constant, that array or dictionary is immutable, and its size and contents cannot be changed." – Misha Birman Jan 16 '15 at 18:05
  • https://itunes.apple.com/us/book/swift-programming-language/id881256329?isInPurchasedView=true&mt=11 – Leo Dabus Jan 16 '15 at 18:07
  • http://en.m.wikipedia.org/wiki/Immutable_object – Leo Dabus Jan 16 '15 at 18:10
  • possible duplicate of [How do you create an immutable array in Swift?](http://stackoverflow.com/questions/24090741/how-do-you-create-an-immutable-array-in-swift) – Kirsteins Jan 16 '15 at 18:14
  • 1
    When Swift was first released, the elements of an immutable array could still be changed as long as you didn't change the length of the array. At some point Apple bowed to the dictates of sanity and reversed that decision, making immutable arrays, truly immutable. – David Berry Jan 16 '15 at 22:03
  • @MishaBirman, did my answer below help you? – gosr Feb 06 '15 at 11:38
  • @eightx2 Yes, your answer helped me. – Misha Birman Jun 25 '15 at 20:54
  • @MishaBirman, in that case, please mark it as the accepted answer. – gosr Aug 15 '15 at 21:39

3 Answers3

1

When you write let, you define an immutable variable. Use var instead; this lets you define a variable that is mutable.

gosr
  • 4,593
  • 9
  • 46
  • 82
1

as stated before me- use a var array if you wish to change your array.

a bit below the question you posted a link and an answer was given inside:

Array in Swift has been completely redesigned to have full value semantics like Dictionary and String have always had in Swift. This resolves various mutability problems – now a 'let' array is completely immutable, and a 'var' array is completely mutable – composes properly with Dictionary and String, and solves other deeper problems. Value semantics may be surprising if you are used to NSArray or C arrays: a copy of the array now produces a full and independent copy of all of the elements using an efficient lazy copy implementation. This is a major change for Array, and there are still some performance issues to be addressed. Please see the Swift Programming Language for more information. (17192555)

0

The use of the let keyword declares a Constant. By definition, a constant cannot be modified.

You want to use the var keyword to declare a Variable, hence things that will/may vary.

From the apple Swift documentation:

Constants and Variables

Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, whereas a variable can be set to a different value in the future.

Declaring Constants and Variables

Constants and variables must be declared before they are used. You declare constants with the let keyword and variables with the var keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made:

let maximumNumberOfLoginAttempts = 10

var currentLoginAttempt = 0

Tokuriku
  • 1,332
  • 13
  • 22