Group Group Group Group Group Group Group Group Group

Chapter 4 - How can I use NSViewControllerRepresentable?

OK. I read this book (SwiftUI by tutorials) several times and now, I wanna make macOS app.

To be honest, I want to implement “Preference” button in ContentView.swift and I already made preference (honestly that’s storyboard
) using “Main.Storyboard” to link shortcut and ViewController that linked with preference.

Following this book (p.73), book’s code should works even considering UI is almost opposite of NS.

Surely I changed all of UI to NS and some things…

here is my source code

struct PreferenceRepresentaion: NSViewControllerRepresentable {

func makeNSViewController(context: NSViewControllerRepresentableContext<PreferenceRepresentaion>) -> NSViewController {
    NSStoryboard(name: "Main", bundle: nil).instantiateController(identifier: "Preference") as! NSViewController

func updateNSViewController(_ nsViewController: NSViewController, context: NSViewControllerRepresentableContext<PreferenceRepresentaion>) {


please help me :frowning:

hi Aksidion! I think where you’re using NSViewController, you should be using your subclass of NSViewController — it might be named ViewController, as it is on page 73.

This code should make your specific view controller representable in SwiftUI.

(ping @kreimben)