私は MacRuby を使用しており、Matt Aimonetti による MacRuby: The Definitive Guide という本を読んでいます。
ムービー CoreData アプリの例では、次のコードを取得しました。
def add_image(sender)
movie = movies.selectedObjects.lastObject
return unless movie
image_panel = NSOpenPanel.openPanel
image_panel.canChooseDirectories = false
image_panel.canCreateDirectories = false
image_panel.allowsMultipleSelection = false
image_panel.beginSheetModalForWindow(sender.window, completionHandler: Proc.new{|result|
return if (result == NSCancelButton)
path = image_panel.filename
# use a GUID to avoid conflicts
guid = NSProcessInfo.processInfo.globallyUniqueString
# set the destination path in the support folder
dest_path = applicationFilesDirectory.URLByAppendingPathComponent(guid)
dest_path = dest_path.relativePath
error = Pointer.new(:id)
NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path, error:error)
NSApplication.sharedApplication.presentError(error[0]) if error[0]
movie.setValue(dest_path, forKey:"imagePath")
})
end
アプリは正常に読み込まれ、問題なく実行されます。CoreData で新しいムービーを作成して削除することもできます。ただし、この関数を呼び出すボタンをクリックすると、ダイアログ ウィンドウが正常に開きますが、「キャンセル」または「ファイルを開く」 " ボタンはここでクラッシュを引き起こします:
#import <Cocoa/Cocoa.h>
#import <MacRuby/MacRuby.h>
int main(int argc, char *argv[])
{
return macruby_main("rb_main.rb", argc, argv); << Thread 1: Program received signal EXC_BAD_ACCESS
}
どんな助けでも大歓迎です。BridgeSupport と関係があると思いましたが、埋め込みが機能しないか、埋め込みを試みても機能しません。いずれにせよ、本で提供されている例もクラッシュするため、他の何かが機能していないようです。
ありがとう!
追記:
macruby.org からこのコードを試してみたところ、問題なく動作しました。
def browse(sender)
# Create the File Open Dialog class.
dialog = NSOpenPanel.openPanel
# Disable the selection of files in the dialog.
dialog.canChooseFiles = false
# Enable the selection of directories in the dialog.
dialog.canChooseDirectories = true
# Disable the selection of multiple items in the dialog.
dialog.allowsMultipleSelection = false
# Display the dialog and process the selected folder
if dialog.runModalForDirectory(nil, file:nil) == NSOKButton
# if we had a allowed for the selection of multiple items
# we would have want to loop through the selection
destination_path.stringValue = dialog.filenames.first
end
end
beginSheetModalForWindow 呼び出しで何かが壊れているか、何かを追跡しようとして何かが欠けているようです。ファイル選択用のモーダル ダイアログを上記のコードで動作させることはできますが、ウィンドウに添付されたシートではありません。