(Huh... having trouble editing this)
I tried fleshing out this code:
//
// AppDelegate.swift
// SimpleMacApp
//
// Created by Eric Mader on 8/26/19.
// Copyright © 2019 Eric Mader. All rights reserved.
//
import Cocoa
import SwiftUI
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
let someObjects: [SomeObject] = [
SomeObject(name: "An object", otherStuff: [
"An object 1",
"An object 2"
]),
SomeObject(name: "Another Object", otherStuff: [
"Another object 1",
"Another object 2"
])
]
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: ContentView(someObjects))
window.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
//
// ContentView.swift
// SimpleMacApp
//
// Created by Eric Mader on 8/26/19.
// Copyright © 2019 Eric Mader. All rights reserved.
//
import SwiftUI
struct ContentView: View {
var someObjects: [SomeObject]
@State var selectedObject: SomeObject?
var body: some View {
HStack {
SomeObjectView(someObjects: someObjects, selectedObject: $selectedObject)
OtherStuff(selectedObject: $selectedObject)
}
}
init(_ someObjects: [SomeObject]) {
self.someObjects = someObjects
}
}
//
// SomeObject.swift
// SimpleMacApp
//
// Created by Eric Mader on 9/2/19.
// Copyright © 2019 Eric Mader. All rights reserved.
//
import Foundation
import SwiftUI
struct SomeObject : Identifiable, Hashable {
let id = UUID()
let name: String
let otherStuff: [String]
}
struct SomeObjectView : View {
var someObjects: [SomeObject]
@Binding var selectedObject: SomeObject?
var body: some View {
List(someObjects, selection: $selectedObject) { someObject in
Text(someObject.name)
}
}
}
//
// OtherStuff.swift
// SimpleMacApp
//
// Created by Eric Mader on 9/2/19.
// Copyright © 2019 Eric Mader. All rights reserved.
//
import SwiftUI
struct OtherStuff: View {
@Binding var selectedObject: SomeObject?
var body: some View {
List(selectedObject?.otherStuff ?? ["(no selection)"], id: \.self) {stuff in
Text(stuff)
}
}
}
This compiles and runs, but the selection doesn't work. Here's a screenshot: