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?

8
  • Immutable usually means read only, I'm not sure why you would be able to mutate (write to) an immutable array Commented Jan 16, 2015 at 17:54
  • developer.apple.com/library/mac/documentation/Swift/Conceptual/… - "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." Commented Jan 16, 2015 at 18:05
  • itunes.apple.com/us/book/swift-programming-language/… Commented Jan 16, 2015 at 18:07
  • en.m.wikipedia.org/wiki/Immutable_object Commented Jan 16, 2015 at 18:10
  • 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. Commented Jan 16, 2015 at 22:03

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.