-1

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?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Jack44
  • 17
  • 6

2 Answers2

1

consoleOuput is declared as an optional (?) and might have nil in it. Despite this, you are force unwrapping it (!) instead of checking for nil. This is the cause of your "fatal error". Any time you forcibly unwrap an optional, you are risking this error.

You might try something more like this:

outputText.stringValue = myDelegate.consoleOutput ?? ""

This will try to unwrap myDelegate.consoleOutput, but if it can't (because it's nil), it will instead just assign the empty string to outputText.stringValue.

The ?? is known as the "Nil Coalescing Operator".

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Thank you, but nothing has changed, still the same error. – Jack44 Mar 27 '15 at 23:02
  • You're also forcibly unwrapping here: `let output:String = NSString(data: data, encoding: NSUTF8StringEncoding)!` and you need to not do that. This is most likely the source of the error. – nhgrif Mar 27 '15 at 23:06
  • When I remove that, I get swift compiler error: `Value of optional type 'NSString?' not unwrapped; did you mean to use '!' or '?'?` – Jack44 Mar 27 '15 at 23:10
0

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 that NSString(data: data, encoding: NSUTF8StringEncoding) is returning nil, so something is wrong with the data pipe.

Samaga
  • 98
  • 5
  • You are right! It prints "Test" perfectly. Although it still gives the same error when I try to display it as `NSTextField.stringValue`. But about the pipe; I can't figure out what's wrong. I took it from [a tutorial](http://square-the-circle.com/2014/08/03/executing-a-system-command-from-a-macos-app-swift/) so it must also be wrong then? – Jack44 Mar 28 '15 at 04:23
  • Are you sure the arguments you are passing to execute command are valid arguments? In other words, when task.launch executes your passed arguments and puts its value in pipe, is it putting valid data there? – Samaga Mar 28 '15 at 04:48
  • Yes, because I can see it executed in console within the debug area. I get the same output from that as I get when I manually enter the code in Terminal. – Jack44 Mar 28 '15 at 04:54