我正在创建一个应用程序,其中使用 NSOpenPanel
,链接到“文件 --> 打开...”菜单。当用户选择 .txt 文件时,程序会读取它,将值添加到现有的字符串数组中,然后应该重新加载 TableView
中的数据。但是,当我在运行时调用tableview.reloaddata()
时,它会给出以下错误:Thread 1: Fatal error: Unexpectedly find nil while隐式解包可选值
>.
这是我的 AppDelegate.swift
文件的代码:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let vc = ViewController(nibName: "ViewController", bundle: nil)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@IBAction func openUnFichier(_ sender: NSMenuItem) {
let fichierPanel: NSOpenPanel = NSOpenPanel()
fichierPanel.allowsMultipleSelection = false
fichierPanel.canChooseFiles = true
fichierPanel.canChooseDirectories = false
fichierPanel.allowedFileTypes = ["txt"]
let response = fichierPanel.runModal()
if response == NSApplication.ModalResponse.OK{
guard let selectedURL = fichierPanel.url else{return}
do{
var fullDocument = try String(contentsOf: selectedURL, encoding: String.Encoding.utf8)
print(type(of: fullDocument))
var lines : [String] = fullDocument.components(separatedBy: "\n" as String)
for line in lines {
vc.test_data.append(line)
print(type(of: vc.test_data))
}
} catch let error as NSError{
print("Erreur!!!!!!! \(error)")
}
vc.tableView.reloadData() //IT CRASHES HERE
}else {
}
}
}
这是我的 ViewController.swift
的代码:
import Cocoa
import WebKit
class ViewController: NSViewController, NSTableViewDataSource{
public var test_data = ["https://www.youtube.com/watch?v=0AQFQMeOAig", "https://www.youtube.com/watch?v=domoD_w3uFw"]
var test_text = ""
var nextUrl = ""
//func
func refresh(){
tableView.reloadData()
}
@IBAction func plus(_ sender: NSButton) {
if urlInput.stringValue == "" {
} else {
test_text = urlInput.stringValue
test_data.append(test_text)
urlInput.stringValue = ""
tableView.reloadData()
// fonction du bouton +
}
}
@IBAction func nextLien(_ sender: NSButton) {
if test_data == [] {
} else {
nextUrl=test_data[0]
var monUrl = URL(string: nextUrl)
var maRequete = URLRequest(url: monUrl!)
view_web.load(maRequete)
test_data.remove(at: 0)
tableView.reloadData()
//fonction du bouton pour le prochain lien
}
}
func numberOfRows(in tableView: NSTableView) -> Int {
return test_data.count
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return test_data[row]
}
//var
@IBOutlet weak var urlInput: NSTextField!
@IBOutlet weak var view_web: WKWebView!
@IBOutlet weak var tableView: NSTableView!
//func vitale
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.youtube.com")
let myRequest = URLRequest(url: myURL!)
view_web.configuration.preferences.plugInsEnabled = true
view_web.load(myRequest)
// Do any additional setup after loading the view.
}
}
我不明白的是, viewtable.reloaddata()
在 ViewController.swift 文件中工作正常,但是当我尝试在AppDelegate
文件。
我确实检查了我的字符串数组(test_data)不为空。 (在我加载并提取我自己创建的“.txt”文件的数据后,它包含 4 个元素。)
我想知道如何修复此错误,以便在解析 txt 文件后数据显示在我的 TableView 中。
非常感谢。
最佳答案
有一个简单的解决方案:将 IBAction
移动到 View Controller 。
- 在 Interface Builder 中,断开
IBAction
与AppDelegate
的连接。 - 将
openUnFichier
方法移至ViewController
(在AppDelegate
中删除它)。 - 在 Interface Builder 中,将菜单项连接到
First Responder
(红色立方体),然后在列表中选择openUnFichier
。响应者链中实现该方法的第一个类将执行该方法。
关于swift - 如何在使用 tableview.reloaddata 时修复 "Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56250096/