Description
Clicking the 👁️ icon in Xcode Debugger displays very useful previews for built-in types. For example it can show a preview of an image, color, cursor, bezier path and many more types.
Here's an example:
Since about 2014 it is possible to implement custom quick look behavior for user defined data types. Here is Apple's official documentation on how to do it.
Unfortunately, as of Xcode 12 custom quick look isn't possible for structs. To the extent of my knowledge the debugQuickLookObject()
method is called only for classes. It requires @objc
decorator responsible for Objective-C bridging in order to work properly. Since TensorFlow.Tensor
is a struct there's no way to implement this feature unless Apple decides to allow it and changes the implementation in Xcode.
Below is an example main.swift
file that demonstrates the problem. When debugging the application QuickLookClass
shows a nice rectangle view after clicking the quick look icon. Whereas QuickLookStruct
doesn't show anything even tough the implementation is identical
import SwiftUI
/// A simple example view.
struct RedRectangleView: View {
var body: some View {
Rectangle()
.fill()
.foregroundColor(.red)
.cornerRadius(5)
}
}
/// A class with working Quick Look in Xcode Debugger.
class QuickLookClass {
init() { }
@objc
func debugQuickLookObject() -> NSView {
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 20, height: 20),
styleMask: [.borderless], backing: .buffered, defer: false)
window.contentView = NSHostingView(rootView: RedRectangleView())
return window.contentView!
}
}
/// A struct with broken Quick Look in Xcode Debugger.
struct QuickLookStruct {
init() { }
func debugQuickLookObject() -> NSView {
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 20, height: 20),
styleMask: [.borderless], backing: .buffered, defer: false)
window.contentView = NSHostingView(rootView: RedRectangleView())
return window.contentView!
}
}
let qlClass = QuickLookClass()
let qlStruct = QuickLookStruct()
print(qlStruct, qlClass)