View GLKit objects in Debug in Swift project

I am working on a Swift/Metal project and want to use the GLKit library for vector and matrix maths. There is no issue integrating all the classes and function. But while debugging, I cannot see any of the properties of the GLKVector and GLKMatrix objects. As I cannot find any posts referring to this, I was wondering if I am missing something. Maybe a bug in XCode 9.4.1?

Replies

The GLKit maths structures are imported from C, and the C types are declared quite weirdly. For example,

GLKVector4
is a typedef for
union _GLKVector4
, and that is defined as a bunch of anonymous cases:
union _GLKVector4
{
    struct { float x, y, z, w; };
    struct { float r, g, b, a; };
    struct { float s, t, p, q; };
    float v[4];
} __attribute__((aligned(16)));

Swift deals with this surprisingly well, but it’s clear that it doesn’t emit enough debugging metadata to allow the debugger to display the contents nicely.

You should definitely file a bug about this; please post your bug number, just for the record.

In terms of workarounds, there’s a bunch of possibilities:

  • It may be possible to work around this using a summary format (control click on the variable in the debugger to see the editor for that). Alas, I wasn’t able to get that to work in a Swift context.

  • You can definitely view the values as a custom type:

    1. Select the variable in the debugger.

    2. Control click and choose View Value As > Custom Type.

    3. Enter a custom type. To continue my

      GLKVector4
      example, this would be
      (v.x, v.y, v.z, v.w)
      .
    4. In the new item, click the disclosure triangle to see the elements.

  • You can print the variable in the debugger console:

    (lldb) p v.x
    (Float) $R10 = 1
    (lldb) p v.y
    (Float) $R12 = 2
    (lldb) p v.z
    (Float) $R14 = 3
    (lldb) p v.w
    (Float) $R16 = 4

    That’s a bit cumbersome, so one option is to extend the type with your own

    CustomDebugStringConvertible
    implementation:
    extension GLKVector4 : CustomDebugStringConvertible {
        public var debugDescription: String {
            return "(\(self.x), \(self.y), \(self.z), \(self.w))"
        }
    }

    You can then print it all at once (using

    po
    , not
    p
    ):
    (lldb) po v
    (1.0, 2.0, 3.0, 4.0)

    .

There may be even better options I haven’t thought of, so I’m looking forward to see if anyone else follows up with some alternative suggestions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for the detailed response. I forgot to mention that I was testing this in 9.4.1. I since have upgraded to 10.0 abd found that the issue is still there. I have reported this as a bug here:

https://bugreport.apple.com/web/?problemID=44685031


For now I can use the immediate window to print the contents.