NSTextView/NSViewController\NSNotification 用于 SWIFT 中 MacOS 应用程序的控制台日志功能

狼星

我正在尝试开发一个应用程序,其中包含一个带有单个 NSTextView 的 StoryBoard,我想用它来记录应用程序事件和用户反馈。我从主视图的复选框开关加载视图(默认情况下,当应用程序启动时,控制台日志视图是打开的)。到目前为止,当应用程序启动并且第二个视图按照 viewDidLoad() 函数的预期处理文本时,一切都运行良好。

控制台日志视图的 NSViewController 类是:

class ConsoleLogViewController: NSViewController,NSTextViewDelegate {

@IBOutlet var textViewConsoleLog: NSTextView!

override func viewWillDisappear() {
    (representedObject as! NSButton).isEnabled = true
    (representedObject as! NSButton).state = NSOffState
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

    textViewConsoleLog.delegate = self
    textViewConsoleLog.string = "All new journal events are logged to this window"
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("*********************************************")
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("\n")
} // END OF viewDidLoad()


func addLogToConsoleWindow(newLogEntry: String) {
    textViewConsoleLog.string? = (newLogEntry)
    textViewConsoleLog.string?.append("\n")
} // END OF addLogToConsoleWindow()

} // ConsoleLogViewController 结束

使用以下函数调用从主视图控制器(下图中名为“Control”)启动视图:

    func showConsoleLogView() {
    let buttonCall = chkBoxConsoleLog
    performSegue(withIdentifier: "sequeConsoleView", sender: buttonCall)
}  //  END OF showConsoleLogView()

在主视图控制器中,我创建了一个 ConsoleLogViewController 实例,然后尝试使用函数 addLogToConsoleWindow 将附加日志信息附加到 textViewConsoleLog。主视图控制器中的相关代码片段是:

    let consoleOutputPrintStatement = ConsoleLogViewController()  // Create an instance of ConsoleLogViewController to allow logging to the new console window

consoleOutputPrintStatement.addLogToConsoleWindow(newLogEntry: "Loading journal files")

但是,当我运行该应用程序并尝试编写额外的文本行时,我得到了一个fatal error: unexpectedly found nil while unwrapping an Optional value. 很明显 textViewConsoleLog 对象为零并且正在创建错误。

我认为问题在于我正在创建该类的另一个实例,因此我试图将其他文本附加到尚未启动的 textViewConsoleLog 对象(或类似的东西!)。

该应用程序首次运行时如下所示:

在此处输入图片说明

那么 - 我是否只是走错了路,试图使用 NSTextView 向控制台开发日志记录功能?或者我关闭但需要使用 NSTextView 的不同方法才能使其工作?如何使用从其他 viewController 生成的相关字符串更新 NSTextField 中的文本?

高度赞赏任何帮助或指导。

狼星

好的 - 我终于使用 NSNotification 整理出了一个解决方案 - 事后看来这是非常明显的。

控制台日志视图中的相关代码如下所示,该类被设置为使用该addObserver方法接收来自 NotificationCenter 的通知

class ConsoleLogViewController: NSViewController,NSTextViewDelegate {

@IBOutlet var textViewConsoleLog: NSTextView!

let controlCentreDidSendConsoleNotification = "TCCEDJ.controlCentreDidSendConsoleNotification"
let controlCentreDidSendConsoleNotificationMessageKey = "TCCEDJ.controlCentreDidSendConsoleNotificationMessageKey"


override func viewWillDisappear() {
    (representedObject as! NSButton).isEnabled = true
    (representedObject as! NSButton).state = NSOffState
}


override func viewDidLoad() {
    super.viewDidLoad()

    textViewConsoleLog.delegate = self
    textViewConsoleLog.string = "All new journal events are logged to this window"
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("*********************************************")
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.string?.append("\n")

    //  set up notification centre
    let notificationCentre = NotificationCenter.default
    notificationCentre.addObserver(self, selector: #selector(addLogToConsoleWindow), name: NSNotification.Name(rawValue: controlCentreDidSendConsoleNotification), object: nil)
} // END OF viewDidLoad()


@objc func addLogToConsoleWindow(newLogEntry: NSNotification) {

    let userInfo = newLogEntry.userInfo as! [String: String]
    let newLogEntryString = userInfo[controlCentreDidSendConsoleNotificationMessageKey]!

    textViewConsoleLog.string?.append(newLogEntryString)
    textViewConsoleLog.string?.append("\n")
    textViewConsoleLog.scrollRangeToVisible(NSMakeRange((textViewConsoleLog.string?.characters.count)! - 1, 0))
} // END OF addLogToConsoleWindow()

} // ConsoleLogViewController 结束

主视图控制器中的相关代码是:

    // Set up variables to use with notification centre
let controlCentreDidSendConsoleNotification = "TCCEDJ.controlCentreDidSendConsoleNotification"
let controlCentreDidSendConsoleNotificationMessageKey = "TCCEDJ.controlCentreDidSendConsoleNotificationMessageKey"

    let testLog = [controlCentreDidSendConsoleNotificationMessageKey: "Test log on button push"]
    let notificationCentre = NotificationCenter.default
    notificationCentre.post(name: NSNotification.Name(rawValue: controlCentreDidSendConsoleNotification), object: self, userInfo: testLog)

所有现在都适用于我正在寻找的行为。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在.NET Core控制台应用程序中获取MacOS用户名?

网络不适用于Visual Studio 2015中的控制台应用程序

pathForResource在Mac OS X控制台应用程序中返回nil — Swift

SqlBulkCopy 用于在 C# 控制台应用程序中具有内部数据表的数据表

在Swift(macOS)中控制背景音乐

Swift中UIWebView中的Javascript控制台日志

如何使用os_log在控制台应用程序中查看iOS设备日志

如何从C#控制台应用程序写入Azure WebJobs中的日志?

从 Ember 等应用程序中删除控制台日志记录

要存储在 Azure 存储中的控制台应用程序日志

我可以在 azure 中查看我的 Web 应用程序的控制台日志吗?

在 .net 控制台应用程序中配置日志记录级别

错误CS0019:运算符'<='无法应用于c#控制台应用程序中类型'double'和'decimal'的操作数

C 中的 FTDI D2XX API 在命令行控制台上工作,但在 macOS 上由 Xcode 和 Swift 编程构建的应用程序中始终返回零

在Azure功能3中,我可以过滤日志以了解应用程序见解和不同级别的控制台

Visual Studio中的.net核心可与控制台应用程序一起使用,但不适用于wpf或winforms

在控制台应用程序中处理行为,自定义功能等的基类?

在 Swift 中的 macOS 应用程序中阻止“您想保存更改吗”对话框

在控制台应用程序中模拟异步死锁

在控制台应用程序中解析粘贴的文本

控制台应用程序中的Autofac(常规)

控制台应用程序中的Linux Qt QSystemTrayIcon

在C#中的控制台应用程序异步?

等待控制台应用程序中的键盘事件

控制台应用程序中的Unity Config错误

在控制台应用程序中显示图像

控制台应用程序中的进度栏

侦听.NET控制台应用程序中的按键

控制台应用程序中的异步等待