2012-01-23 40 views
2

是否可以使用Ruby的Cocoa API将这些库加载到FFI中?我知道你可以使用RubyCocoa或MacRuby访问这些库,但如果可能的话,我宁愿坚持“普通”的ruby。我已经读过RubyCocoa使用libffi(如果我没有误认为FFI依赖的话)来使用这些库,所以也许可以用FFI做同样的事情。使用FFI从Ruby访问可可?

谢谢!

回答

1

是的,但不是非常有用的方式。

这里是一个全功能的例子:

require 'ffi' 

module ObjC 
    extend FFI::Library 

    ffi_lib 'objc' 

    attach_function :sel_registerName, [:string], :pointer 
    attach_function :objc_msgSend, [:pointer, :pointer, :varargs], :pointer 
    def self.msgSend(id, selector, *args) 
    selector = sel_registerName(selector) if selector.is_a? String 
    return objc_msgSend(id, selector, *args) 
    end 
    attach_function :objc_getClass, [:string], :pointer 
end 

module Cocoa 
    extend FFI::Library 

    ffi_lib '/System/Library/Frameworks/Cocoa.framework/Cocoa' 

    attach_function :NSApplicationLoad, [], :bool 
    NSApplicationLoad() 
end 

pool = ObjC.msgSend(ObjC.msgSend(ObjC.objc_getClass("NSAutoreleasePool"),"alloc"),"init") 
application = ObjC.msgSend(ObjC.objc_getClass("NSApplication"),"sharedApplication") 
ObjC.msgSend(application,"run")