当应用程序在 macOS 上关闭时在 @EnvironmentObject 上迭代数组
Iterating an array on an @EnvironmentObject when the application closes on macOS
我有一个 @EnvironmentObject
为我的主视图提供一个数组。声明如下:
my_app.swift
@main
struct My_AppApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataModel())
}
}
}
ContentView.swift
struct NoteItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
var date = Date()
var changed: Bool = false
}
final class DataModel: ObservableObject {
@AppStorage("mytestapp") public var notes: [NoteItem] = []
init() {
self.notes = self.notes.sorted(by: {
[=11=].date.compare(.date) == .orderedDescending
})
}
}
我从ContentView.swift中的不同观点称呼这个为:
struct AllText: View {
@EnvironmentObject private var data: DataModel
}
我在 my_app.swift 中添加了检测用户何时关闭应用程序的功能,以便我可以执行一些操作。
#if os(macOS)
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
// trying to iterate on the struct within DataModel() here
print("app closing")
}
}
#endif
@main
struct My_AppApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataModel())
}
}
}
现在,我正在尝试访问 DataModel()
中的结构,这样我就可以检查每个元素是否都有更改集,但无论我尝试什么,或者我如何声明 environmentObject,我都会得到 segfault
,或 No ObservableObject of type DataModel found. A View.environmentObjectfor DataModel may be missing as an ancestor of this view.
等错误
如何访问该 DataModel 并对其进行迭代,以便在关闭应用程序时执行操作?
谢谢。
这是可能的方法 - 在 ContentView
出现时注入数据模型,例如
#if os(macOS)
class AppDelegate: NSObject, NSApplicationDelegate {
var dataModel: DataModel? // << here
func applicationWillTerminate(_ aNotification: Notification) {
print("app closing")
// use self.dataModel? here
}
}
#endif
@main
struct My_AppApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
private let dataModel = DataModel() // << here !!
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(self.dataModel) // << here !!
.onAppear {
#if os(macOS)
appDelegate.dataModel = self.dataModel // << here !!
#endif
}
}
}
}
我有一个 @EnvironmentObject
为我的主视图提供一个数组。声明如下:
my_app.swift
@main
struct My_AppApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataModel())
}
}
}
ContentView.swift
struct NoteItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
var date = Date()
var changed: Bool = false
}
final class DataModel: ObservableObject {
@AppStorage("mytestapp") public var notes: [NoteItem] = []
init() {
self.notes = self.notes.sorted(by: {
[=11=].date.compare(.date) == .orderedDescending
})
}
}
我从ContentView.swift中的不同观点称呼这个为:
struct AllText: View {
@EnvironmentObject private var data: DataModel
}
我在 my_app.swift 中添加了检测用户何时关闭应用程序的功能,以便我可以执行一些操作。
#if os(macOS)
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
// trying to iterate on the struct within DataModel() here
print("app closing")
}
}
#endif
@main
struct My_AppApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataModel())
}
}
}
现在,我正在尝试访问 DataModel()
中的结构,这样我就可以检查每个元素是否都有更改集,但无论我尝试什么,或者我如何声明 environmentObject,我都会得到 segfault
,或 No ObservableObject of type DataModel found. A View.environmentObjectfor DataModel may be missing as an ancestor of this view.
如何访问该 DataModel 并对其进行迭代,以便在关闭应用程序时执行操作?
谢谢。
这是可能的方法 - 在 ContentView
出现时注入数据模型,例如
#if os(macOS)
class AppDelegate: NSObject, NSApplicationDelegate {
var dataModel: DataModel? // << here
func applicationWillTerminate(_ aNotification: Notification) {
print("app closing")
// use self.dataModel? here
}
}
#endif
@main
struct My_AppApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
private let dataModel = DataModel() // << here !!
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(self.dataModel) // << here !!
.onAppear {
#if os(macOS)
appDelegate.dataModel = self.dataModel // << here !!
#endif
}
}
}
}