假设您想打开一个简单的警报框,在objective-c 世界中将类似于:
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:@"Alert."];
[alert beginSheetModalForWindow:window
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
beginModalForWindow 被定义为一个选择器方法。在苹果参考指南中,它的全名是“beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:”
它在 NSAlert.h 中定义为:
- (void)beginSheetModalForWindow:(NSWindow *)window modalDelegate:(id)delegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo;
现在简单的问题,如何在 ruby ffi 中定义这个方法?
module AppKit
extend FFI::Library
# Load the Cocoa framework's binary code
ffi_lib '/System/Library/Frameworks/AppKit.framework/AppKit'
attach_function :beginSheetModalForWindow, [:pointer,:pointer,:pointer], :bool
end
失败:
An exception occurred running ffi-test.rb
Function 'beginSheetModalForWindow' not found in [/System/Library/Frameworks/AppKit.framework/AppKit] (FFI::NotFoundError)