Search code examples
arraysswiftswift-playground

Code in Swift Playground not showing data inside Array


Does anyone know the legit first setting for learning code in Xcode? The data on the right side not showing inside an Array but only the type data inside it.

Currently learning it, usually I do code in VScode.

Result of the setting, like a guide.

Image of xcode preview


Solution

  • In older versions of Xcode, the contents of arrays was indeed shown on the right sidebar. See the screenshot from this question.

    In the Xcode 15 release notes, there was a whole section about Playgrounds, which mentioned:

    The result sidebar in Xcode Playgrounds shows summaries for all expressions on the line, and a new control allows you to see a popover with details about each expression.

    "Array of 3 string elements" indeed sounds like a "summary" for the expression, and you can indeed click on the eye button on the right to see a popover (or the bordered square button on the left to show an inline panel), containing a structured description.

    enter image description here

    Output:

    enter image description here

    This looks like it is intended this way. Each element is displayed in its own line, together with the element's index. If you find this hard to read, you can conform Array<String> to CustomPLaygroundDisplayConvertible, and return a string (or anything else that playgrounds support displaying) there.

    For example, here I implemented it so that the array's elements are all joined together in a single String, with , separators.

    extension Array: CustomPlaygroundDisplayConvertible where Element == String {
        public var playgroundDescription: Any {
            self.joined(separator: ", ")
        }
    }
    

    Now the display on the right is a lot easier to read.

    enter image description here

    Do be careful that doing this will affect the display of all Array<String> (or whatever type the extension is all).