UIKit Live Preview for Objective-C View Controller?

Now that live previews are available in UIKit (source: https://developer.apple.com/wwdc23/10252)

I was wondering how to get a UIViewController from Objective-C. The Swift macro looks like this:

#Preview {
  var controller = SomeViewController()
  return controller;
}

Is there a way to get a live preview for UIViewControllers/UIViews written in Objective-C (other than wrapping it as a child view controller in an empty swift view controller)?

I’ve not tried this, and UI frameworks are not my area of expertise, but there is an Editor > Expand Macro command you could use to see what the macro is actually doing.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for that tip. It doesn't appear to be possible to create a live preview purely from Objective-C unfortunately. When I expand the macro I see it uses API declared in the DeveloperToolsSupport.framework which is Swift only and everything is Swift structs and Swift protocols not exposed to Objective-C.

You can create a live preview for an Objective-C view controller/view in a Swift source file...by importing the Objective-C header and then create the Objective-C view controller from a Swift source file in the Preview macro:

//In a Swift source file (make sure header file is imported for the view controller/view in the bridging header)
#Preview {
    let someVC = ObjCViewController()
    return someVC
}

This seems kind of silly and dev hostile IMO. The preview works but you have to jump back and forth between the Objective-C .m and the Swift file created only for the purpose of making the live preview.

It's true that it's not possible to make a Preview in Objective-C, you will need a Swift file to house the preview. But, when used in conjunction with pinning you don't need to switch back and forth between the Swift and the Objective-C.

Here's what I did:

  1. Made a new view controller in Objective-C
  2. Imported the Objective-C view controller's header in my app's bridging header
  3. In a Swift file, made a #Preview that instantiated the view controller
  4. Clicked the "Pin" button in the upper left of the preview canvas
  5. Went back to the Objective-C file: the preview stays up
  6. Edited the Objective-C code. Hit Command-S to trigger the preview to update (it will happen on auto-save too, but that might not be as immediate as you want)

@previewseng Nice tip. That does work and will have to do for now when I want to use a Live Preview.

I get great compiler performance and it never crashes for ObjC so it's kind of a shame I have to pollute my code base with Swift for a design time feature (** JOKING**. I'm only JOKING don't kill me Swifters).

UIKit Live Preview for Objective-C View Controller?
 
 
Q