Search code examples
swiftswift-playground

What does {{{…}}} mean in Swift?


I wrote the following code in playground.

struct Product {

    var randomNumber: Int?
    init?(number: Int){
        if number <3 {return nil}
        randomNumber=number

    }
}
var mug = Product(number: 5)        // {{{…}}}

In the output on the right I get {{{…}}} as indicated by the comment. What do this symbols mean?


Solution

  • It's a shorthand notation that indicates different levels of wrapping. Xcode is just telling you what's in the variable mug The outermost curly-brackets are the wrapping around the optional. The second level of brackets wraps around your struct properties and the third level of wrapping is an optional wrapped around your randomNumber property. You can visualize it by doing this.

    var mug = Product(number: 5) // {{{...}}}
    var amug = mug! // {{Some 5}}
    var bmug = mug!.randomNumber // {Some 5}
    var cmug = mug!.randomNumber! // 5