Posts

Post not yet marked as solved
1 Replies
I did some playing around and found that the identifiers I want are "public.truetype-ttf-font", "public.truetype-collection-font" and "public.opentype.font". With these I can define document type properties and imported type identifiers for the font files.
Post not yet marked as solved
2 Replies
OK, I give up. It won't take the screenshot. :-(
Post not yet marked as solved
2 Replies
Grrr... the screenshot didn't post. I'll try again:
Post not yet marked as solved
3 Replies
Thanks. Making the selection binding value be an Optional makes some kind of sense.My guess about the id: parameter is that the id is saved for each entry in the list and used as the selection value. Thus it must match the type of the selection: argument.
Post not yet marked as solved
3 Replies
I made this little app based on a post in StackOverflow:struct SomeName : Identifiable, Hashable { let id = UUID() let name: String}var someNames = [ SomeName(name: "Ron Swanson"), SomeName(name: "April Ludgate"), SomeName(name: "Andy Dwyer"), SomeName(name: "Leslie Knope"), SomeName(name: "Tom Havaford"), SomeName(name: "Jerry Gergich")]struct ContentView : View { @State var selectKeeper: SomeName? var body: some View { List(someNames, id: \.self, selection: $selectKeeper) { someName in Text(someName.name) }.frame(width: 500, height: 460) }}When I run this and click on one of the names, it will highlight, so I assume it's getting selected. I deleted "id; \.self" thinking it was redundant given that SomeName is Identifiable, and it stopped working. I then added "id: \.self" to the List in SomeObjectView in this post:https://forums.developer.apple.com/thread/122140Once I did that, that little app started working! So somehow, the "id: \.self" is key. Does anyone understand why?
Post not yet marked as solved
9 Replies
It's initialzied in AppDelegate.swift. (I know, it's kind of lame)
Post not yet marked as solved
9 Replies
I tried following the example in the article about NavigationView:// // 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 { NavigationView { List(someObjects) { someObject in NavigationLink(destination: OtherStuff(selectedObject: someObject)) { Text(someObject.name) } } .navigationBarTitle(Text("Objects")) } } }This doesn't compile. It highlights the "n" in someObject.name and says "Type of expression is ambiguous without more context." It compiles fine if I remove the NavigationView and NavigationLink.
Post not yet marked as solved
9 Replies
Huh... it didn't show the screenshot either. :-(The screenshot just shows the two views. The OtherStuff view just shows "(no selection)." Selecting in the SomeObjectView doesn't seem to have any effect.
Post not yet marked as solved
9 Replies
(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 Cocoaimport SwiftUI@NSApplicationMainclass 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 SwiftUIstruct 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 Foundationimport SwiftUIstruct 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 SwiftUIstruct 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:
Post not yet marked as solved
9 Replies
Thanks for the reply.I tried your suggestion, and indeed, ContentView compiles. However, I get the an error in AppDelegate saying that selectedObject: is missing from the call to ContentView(). Couldn't figure out how to fix this. Maybe I don't really understand @Binding?To be honest, I couldn't work out from the doc what a NavigationView does, so I just tried it. I got a view that looked like that top part of a TabView.This is a Mac App, and I already have a view to show the selected SomeObject, I just want it to update when the selection in ContentView changes.
Post not yet marked as solved
9 Replies
I realize I didn't say what I want out of the selection. I could use either the selected SomeObject, or the index into the someObjects array. I want to display the object in another view.
Post not yet marked as solved
2 Replies
I was able to answer the first question by generating an empty document based App and looking at how it is set up.I read about opague types and "some" in the Swift 5.1 manual and thought that I could solve this problem by ovinb the Text() View into another View. Both views still have differnt types, and so it still doesn't compile...