0

私は 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 呼び出しで何かが壊れているか、何かを追跡しようとして何かが欠けているようです。ファイル選択用のモーダル ダイアログを上記のコードで動作させることはできますが、ウィンドウに添付されたシートではありません。

4

1 に答える 1

1

NSOpenPanel は NSSavePanel のサブクラスであるため、NSSavePanel のbeginSheetModalForWindow:completionHandler:. それrunModalForDirectoryが減価償却です。ただし、「減価償却」は「動かない」という意味ではなく、「将来動かなくなる」という意味です。

発生しているエラーは、MacRuby 自体をロードする C コードを指しています。これは、MacRuby がトラップできなかった深刻なクラッシュを示しています。デバッガーがエラーが発生したスタックをトラップできる唯一の場所であるため、Main.m に表示されるだけです。残念ながら、このようにスタックの一番上/一番下にエラーを配置すると、デバッグには役に立たなくなります。

Matt Aimonetti のコードには明らかな問題は見られないので、完了ハンドラに渡されたブロックを処理する MacRuby に問題があると推測します。これは、ブロックがそれを定義するオブジェクトとは異なるアドレス空間にあるため、エラーがトラップされない理由も説明します。

ブック サイトまたはMacRuby メーリング リストから Matt Aimonetti に直接連絡することをお勧めします(彼はそこで活発に活動しています)。

于 2011-07-01T16:11:13.157 に答える