From: Satoshi N. <sna...@in...> - 2007-07-15 15:19:31
|
Hi Laurent. > objc_method :getInt, ['int'] > > The second argument of objc_method is supposed to be an Array. I will > fix the method to accept a String. I think it might be difficult. Because the second argument accepts a string in raw encodings like 'i@:'. -- Satoshi Nakagawa On 2007/07/15, at 22:58, Laurent Sansonetti wrote: > Oops, please try > > objc_method :getInt, ['int'] > > The second argument of objc_method is supposed to be an Array. I will > fix the method to accept a String. > > Laurent > > On Jul 15, 2007, at 3:55 PM, Motohiro Takayama wrote: > >> Laurent: >> >> Thanks for your quick reply. >> >> I tried your advice, and observed strange behavior. >> >> class Hoge < OSX::NSObject >> objc_method :getInt, 'int' >> def getInt >> 5 >> end >> end >> >> and when call this, NSLog shows: >> >> 2007-07-15 22:50:15.667 retval[7097] XXX returning ffi type void for >> unrecognized encoding 'nt' >> >> I'm wondering why "i" of int is lack... >> >> 2007/7/15, Laurent Sansonetti <lsa...@ap...>: >>> When creating a Ruby method like this, without providing its ObjC >>> runtime signature, RubyCocoa will assume that all arguments and the >>> return value are Objective-C objects (of the type 'id'). Because >>> there >>> is no way for RubyCocoa to know. This is why you get from the ObjC >>> world an NSNumber and not an "int". >>> >>> To work around this, you need to register the Ruby method into the >>> ObjC runtime: >>> >>> def getInt ... end >>> objc_method :getInt, 'int' >>> >>> This will register getInt as a method that doesn't take any argument >>> and returns a C integer. >>> >>> Usually when you are overriding an ObjC method from Ruby, or >>> implementing an ObjC informal protocol in Ruby, you don't need to use >>> objc_method. But in this special case, it is required. Fortunately, >>> most people won't need to use it. >>> >>> Laurent >>> >>> On Jul 15, 2007, at 3:01 PM, Motohiro Takayama wrote: >>> >>>> Hi, >>>> >>>> I'm wondering how to return simple "int" type value from Ruby code >>>> to >>>> Objective-C code. >>>> Ruby code passes Fixnum, and Objective-C code recieves NSCFNumber >>>> instead of simple int. >>>> Any suggestion ? >>>> >>>> >>>> Ruby code: >>>> >>>> class Hoge < OSX::NSObject >>>> def getInt >>>> 5 >>>> end >>>> end >>>> >>>> >>>> Objective-C code: >>>> >>>> Class helper = NSClassFromString(@"Hoge"); >>>> id hoge = [[helper alloc] init]; >>>> NSLog(@"%@ %d", [[hoge getInt] class], [(NSNumber *)[hoge >>>> getInt] >>>> decimalValue]); >>>> >>>> >>>> and those show: >>>> >>>> 2007-07-15 21:52:56.954 retval[5100] NSCFNumber 8448 >>>> >>>> >>>> thanks, >>>> >>>> -- >>>> Motohiro Takayama >>>> http://blog.deadbeaf.org/ |