1

I've written this code in a .playgraound

var a = [1, 2, 3]
var b = a
var c = a

if b === c
{
    "b and c still share the same array elements."
}
else
{
    "b and c now refer to two independent sets of array elements."
}

The result is "b and c now refer to two independent sets of array elements" but in "The Swift Programming Language" Apple says that

The example below uses the “identical to” operator (===) to check whether b and c still share the same array elements.

Can you explain me why they are different?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Charlotte
  • 135
  • 2
  • 10

2 Answers2

2

The reason the book says

The result is "b and c now refer to two independent sets of array elements"

is that the code earlier in the book stopped array sharing between b and c established by the assignment of a to both of them:

b.unshare() // Page 306, line 3

Array a has been unshared from b and c implicitly by appending an element to it on page 305, line 1.

With the code as you show the "b and c still share the same array elements." message will be printed.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    What about this. var a : Array = [1, 2, 3] if a === a { println("b and c still share the same array elements.") } else { println("b and c now refer to two independent sets of array elements.") }. it still returs false a === a. – Daniel Jun 08 '14 at 19:46
0

b === c tests what will happen to c if you change one of the elements of b or vice versa. In your example, b === c evaluates to true, so when you change an element of b:

var a = [1, 2, 3]
var b = a
var c = a

b[1] = 10

you see the change reflected in c:

c[1] // returns 10

You can use the unshare() method to ensure that b refers to an independent array instance:

b.unshare()
b === c // returns false

b[2] = 10
c[2] // returns 3
nathan
  • 5,466
  • 3
  • 27
  • 24
  • 1
    `b === c` returns false in his example. `b` and `c` refer to two different copies of `a` so changing `b` does not reflect a change in `c` – drewag Jun 08 '14 at 19:47
  • No, it doesn't. Write `var a = [1, 2, 3]; var b = a; var c = a; b === c` in a new Playground. The last expression will evaluate to `true`. – nathan Jun 08 '14 at 19:49
  • 1
    I copied that exact code into a playground and b === c is returning false for me. But I agree that according to the book it should return true so I would remove the down vote but I can't unless you edit your answer. – drewag Jun 08 '14 at 19:54
  • That's odd... Are you using swift-600.0.34.4.5, and did you have any other code in the playground? – nathan Jun 08 '14 at 19:57
  • 1
    If I run it in the REPL (swift-600.0.34.4.5) it returns true. I don't know how to determine the version in the playground. (no there is not any other code in the playground) – drewag Jun 08 '14 at 19:59
  • Just noting that I had the exact same results in playground. The identity operator returns false for b === c. – memmons Jun 08 '14 at 20:02
  • Is this an issue with the compiler, then? We all copy-pasted the same code into the same version and we got different results... – nathan Jun 08 '14 at 20:05
  • Possibly. I'm going to leave my answer up and see what shakes out after the next few weeks. – memmons Jun 08 '14 at 20:08
  • @nathan I think it is an issue with the compiler but it seems that the book says it should return true. My intuition says that it should return false because arrays are value types, but there is definitely something strange going on – drewag Jun 08 '14 at 20:11