I am trying to change the content of NSTextField in the ViewController by changing its stringValue coming from AppDelegate, but it gives me an error.
The code in AppDelegate is:
class AppDelegate: NSObject, NSApplicationDelegate {
var consoleOutput:String? = "Console Output"
func executeCommand(command: String, args: [String]) -> String {
let task = NSTask()
task.launchPath = command
task.arguments = args
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output:String = NSString(data: data, encoding: NSUTF8StringEncoding)!
consoleOutput = output
return output
}
}
It is called from the ViewController:
var myDelegate = NSApplication.sharedApplication().delegate! as AppDelegate
and the NSTextField is an IBOutlet created earlier:
@IBOutlet weak var outputText: NSTextField!
and tried to modify later:
outputText.stringValue = myDelegate.consoleOutput!
But all I get is
fatal error: unexpectedly found nil while unwrapping an Optional value
What am I doing wrong?
Try setting this line:
let output:String = NSString(data: data, encoding: NSUTF8StringEncoding)!to:let output: String = "Test"If this eliminates the error, then you know thatNSString(data: data, encoding: NSUTF8StringEncoding)is returning nil, so something is wrong with the data pipe.