How do I access a returned value from a Process(), in this case 'which'...
- var sips_path : String?
- //MARK: locate sips on local machine
- let which_sips = Process()
- which_sips.executableURL = URL(fileURLWithPath: "which")
- which_sips.arguments = ["sips"]
- do { sips_path = try which_sips.run() }
- catch let error as NSError { sips_path = "/usr/bin/sips"; print("Failed to execute which_sips", error) }
line 8. gets compiler error "Cannot assign value of type '()' to type 'String?'"
I believe, but cannot prove, 'which' returns a string. .run() throws and throws are for errors only, right? So where is the result of calling which?
It seems I should use a closure to use $0 but it's already in one...
line 9. intends to assign a default path.
it’s not obvious why you’re seeing the problem while I’m not.
So I dug deeper into this issue and have a status update…
To start, after looking into this in a lot more detail I’ve concluded the the final line in my code:
pipe.fileHandleForWriting.closeFile()
is, as eaigner suspected, incorrect. I still don’t know why it crashes for eaigner (and others!) but not for me, but I’m confident that it shouldn’t be there.
Anyway, I recently had cause to research this issue in a lot more depth, and I’ve put (what I hope is) my final answer in a new post, Running a Child Process with Standard Input and Output. Please read it through and post back here if you have further questions or comments.
Also, if you’re curious as to why I abandoned FileHandle
here, see Whither FileHandle?.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"