From: Laurent S. <lsa...@ap...> - 2007-07-15 13:38:49
|
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/ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Rubycocoa-talk mailing list > Rub...@li... > https://lists.sourceforge.net/lists/listinfo/rubycocoa-talk |