Empty Square Brackets in Output

Hello reader.
The question is easy, so let me not beat around the bush.
Question: Why were the square brackets printed? How to avoid it?
Note: The struct has no particular purpose but demonstration. Code above has no affect on the struct, which is obvious.

struct NewestStruct {
     var num: Int {
        didSet{
            print("Old value \(num)")
        }
    }
}
var newOne = NewestStruct(num: 0 )
for i in 1...10 {
    print(newOne.num = i)
}

Thank you in advance.

Hi @Dennis411

On line 438, you're trying to print the result of the assignment newOne.num = i. However, in Swift, assignments don't return a value. They are said to return Void, which is printed as an empty tuple ().

I'm guessing you just want to print the value? If so, you only need to do this:

struct NewestStruct {
     var num: Int {
        didSet{
            print("Old value \(num)")
        }
    }
}
var newOne = NewestStruct(num: 0 )
for i in 1...10 {
    newOne.num = i
}

I should also note that what you're printing is in fact the value that was set, not the old value.

1 Like

Void. Of course , I should have guessed by myself :sweat_smile:. Thanks a lot.

I used "print" twice, my bad. :man_facepalming:

Terms of Service

Privacy Policy

Cookie Policy