でいくつかのプロットを作成するために、swift から gunplot を呼び出そうとしています.png
。ただし、以下のプログラムは機能しません --- 結果の1.png
ファイルは空です。ただし、「set term aqua」をそのままにしておくと、プロットを含む aqua ウィンドウが呼び出されます。ただし、出力をファイル (png または pdf) に設定しようとすると、結果は常に空のファイルになります。
gnuplot コマンドは問題ありません --- 手動で正しく実行できます。
import Foundation
let task = NSTask()
task.launchPath = "/opt/local/bin/gnuplot"
task.currentDirectoryPath = "~"
let pipeIn = NSPipe()
task.standardInput = pipeIn
task.launch()
let plotCommand: NSString =
"set term png\n" +
"set output \"1.png\"\n"
"plot sin(x)\n" +
"q\n"
pipeIn.fileHandleForWriting.writeData(plotCommand.dataUsingEncoding(NSUTF8StringEncoding)!)
私はswiftとpipesの両方に慣れていないので、コードに何か問題があるか、推奨されていないか教えてください。