###前提・実現したいこと
Xcode9からプロジェクトがデフォルトでSandboxとなったのでしょうか、これまでできていた外部へのファイル書き出しができなくなり、色々調べた結果NSSavePanelを利用することにしました。が、うまく機能しません(ダイアログが開かない)。
初歩的で申し訳ありませんが、よろしくおねがいします。
###発生している問題・エラーメッセージ
Playgroundだとなんとか成功しますが、実装しようとするとエラーになります(ダイアログが開かない)。
また、Playgroundでも書き出せてはいるものの謎のメッセージが表示されています。
//Cocoa appで実装した際のエラー(長すぎるので抜粋) ImportFile[1225:60723] warning: <NSRemoteView: 0x608000121c20> determined it was necessary to configure <NSVBSavePanel: 0x101119300> to support remote view vibrancy 2017-10-13 12:20:20.149855+0900 ImportFile[1225:60723] -[NSVBSavePanel init] caught non-fatal NSObjectNotAvailableException 'service com.apple.appkit.xpc.openAndSavePanelService failed to init an instance of NSSavePanelService on behalf of <NSRemoteView: 0x608000121c20> due to { ...
//Playgroundで実行した際に表示されるメッセージ(全文) Oct 13 12:24:32 [942] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 13 12:24:32 [942] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 13 12:24:32 [942] <Error>: CGContextGetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 13 12:24:32 [942] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 13 12:24:32 [942] <Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 13 12:24:32 [942] <Error>: CGContextSetCompositeOperation: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Playgroundで表示されているメッセージは調べたらどうやらGUI関係のもののようで、なぜここでそういうエラーがでるのか全くわからずお手上げです...
###該当のソースコード
let savePanel = NSSavePanel() savePanel.runModal() if let url = savePanel.url{ let text:String = "書き出し内容" do { try text.write( to: url, atomically: false, encoding: String.Encoding.utf8 ) } catch { print(error) } }
###試したこと
NSOpenPanelも実装したのですが、こちらはうまくいっています。
NSSavePanelに関しては以下のページを参考にトライしてみましたが、ダメでした。
http://bricho-swift.blogspot.jp/2016/03/os-x.html
https://qiita.com/hanamiju/items/d10524e8650ae171fa2e
https://qiita.com/takecian/items/d31682899768f89a8d80
Appleの公式ドキュメントもトライしてみたのですが、ハードルが高かったです...
https://developer.apple.com/documentation/appkit/nssavepanel#//apple_ref/occ/cl/NSSavePanel
###補足情報(言語/FW/ツール等のバージョンなど)
Swift4, Xcode9.0, macOS Sierra 10.12.6