swiftxcode

Why does calling map() on an array yield an extraneous value in an Xcode Playground


My code:

import UIKit

let arr: [Int] = [1, 2, 3]
let result = arr.map { x in x + 2 }
result

Here is what an Xcode 16.2 Playground shows for the results:

enter image description here

Where does the extra 5 come from on line 4?

adsfhasdkfkahjsdflasdhhf asdfasdklfhkasdjhfksadhf asdfkhasdfkhjasdflkasdfhksd


Solution

  • You can click on the rectangle beside that result to expand it. It is actually two results squished into one line.

    enter image description here

    By hovering over each of those squares, parts of your code get highlighted to indicate what the result is referring to. "[3, 4, 5]" highlights the word "result" in let result = ...

    enter image description here

    and "5" highlights x + 2.

    enter image description here

    This is the value of x + 2 the last time it has been evaluated. To see the other values of x + 2, click on the square next to "5", and it will display a graph showing the value of x + 2 over time. By clicking on a point in the graph, you can read its value.

    enter image description here