0

For a struct defined as below, I am looking for a method that would print all the members of the struct, including the bitfields.

public struct A {

    public var privateVar0 : UInt32
    public var bit_1 : UInt32 {
        get {
            return privateVar0 >> 0 & 0b00000000000000000000000000000001
        }
    }

    public var bit_2 : UInt32 {
        get {
            return privateVar0 >> 1 & 0b00000000000000000000000000000001
        }
    }

    public var bit_3 : UInt32 {
        get {
            return privateVar0 >> 2 & 0b00000000000000000000000000000001
        }
    }
}

Expected Output:

privateVar0 : <Value>
bit_1 : <Value>
bit_2 : <Value>
bit_3 : <Value>

I have tried to use the dump() in-built API that uses Mirror, but it prints only "privateVar0" and not the bitfields.

SKK
  • 1
  • 1
    `bit_X` are computed properties. They are not stored as part of the struct. – Sweeper Feb 01 '22 at 16:49
  • Maybe some look there https://stackoverflow.com/questions/26181221/how-to-convert-a-decimal-number-to-binary-in-swift ? You can also make your structure conforms to CustomStringConvertible – Larme Feb 01 '22 at 16:50
  • 1
    As Sweeper said, these properties are not in the actual `A` struct. Also, you can clean this up a bit by replacing the `0b00000000000000000000000000000001` with just `1` since it conveys the same meaning and shorter. Less prone to mistakes if you accidentally delete a `0`, for example. – George Feb 01 '22 at 18:10

0 Answers0