Hi All,
I 'm developing a SwiftUI based Mac app that amongst other things has a text editor. The editor is subclassed from NSTextView and some additional functionality added. It is also wrapped in a NSViewRepresentable to make it a SwiftUI View. I'd like to add a standard Find/Replace functionality to it - menu items, find/replace bar or panel, and etc.
I did read the documentation on NSTextFinder and NSTextFinderClient, but it's kind of hard to see the full picture how these classes should be used together. Also, since this is SwiftUI based app, I'm not using story boards.
If you know any examples, tutorials, or just code snippets that could help me get start, please share. Amy links are greatly appreciated.
Thanks!
Post
Replies
Boosts
Views
Activity
Hi,
How can I use a Table view to build a data grid for a dynamic set of data?
Say, I have a 2D collection of data elements (a matrix, basically) with variable number of columns, and a 1D collection of column labels (a vector). For the sake of simplicity, let's assume that all data elements are Strings.
let data: [[String]];
let columnLabels: [String]
How can I build TableColumn definitions without key paths?
Thanks!
Hello,
I'm building a multi document app for macOS, in which a document can be linked with exactly one Sqlite data store. All stores have the same data model (same entities, attributes, and etc) but different data. Each data store is a persistent cache for a remote database.
It all works fine with one open document. Once I open a new document, the app crashes with the following error:
2021-12-02 13:37:45.991866-0800 MacOra[22562:632339] [error] error: +[DBObject entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
The App is set up as follows:
var body: some Scene {
DocumentGroup(newDocument: { Document() }) { config in
DocumentView(document: config.document)
// injecting persistent controller into the document view
.environment(\.managedObjectContext, config.document.persistentController.container.viewContext)
The Document is a class ViewModel.
class Document: ReferenceFileDocument {
@Published var persistentController: PersistenceController
required init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents
else {
throw CocoaError(.fileReadCorruptFile)
}
// linking data store
persistentController = PersistenceController(name: myStoreName)
}
Finally, here is the PersistenceController. I intentionally don't use a singleton here because I want each document to have its own independent persistence controller linking the document to its data store.
struct PersistenceController {
// don't want to make it a singleton as each document should have it's own store
// static let shared = PersistenceController()
let container: NSPersistentContainer
init(inMemory: Bool = false, name: String = "preview") {
// initializing data model
guard let modelURL = Bundle.main.url(forResource: "MyModel", withExtension: "momd") else {
fatalError("Error loading model from bundle")
}
let dataModel = NSManagedObjectModel(contentsOf: modelURL)!
container = NSPersistentContainer(name: name, managedObjectModel: dataModel)
// <<<<<<< this it where it fails
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
}
I kind of understand that CoreData creates a second set of entities when I open a new document, and it gets lost because of that. But I don't understand why as I'm setting up a new instance of everything specifically to avoid this issue.
Is this design not feasible with CoreData at all? How would one go about having an MDI app with multiple stores with the same structure?
I'd like to avoid having one big store with all data for all documents because of performance reasons as each data store can get quite large.
Thanks!
Hello,
According to the documentation, I could use this method to override the default calculation of a column width based on cells width. The doc says it's using a random sampling for large tables.
I have a view based table, and it just doesn't work - not for small tables (dozes of rows), not for large tables (thousands of rows.)
Also, I see this method is only invoked when I doubleckick on a column separator element (resize cursor), not automatically when the table is populated. Could you please confirm?
Thanks!