Oh I see, indeed. The problem here is that the contextInfo argument has a "void *" type, so the runtime does not know that you passed an Objective-C object for it via an implicit cast.

In this case, we need to introduce a new method on Pointer to allow you to change its type. In RubyCocoa, the method is #cast_as, I will probably use the same name.

In practice, you should avoid passing objects as context to methods like this one, because their implementation won't keep a GC reference to them, so they might disappear during a collection cycle.

Laurent

On Dec 30, 2008, at 7:52 PM, Nic Williams wrote:

Calling recipe[0] results in this error:

AppDelegate.rb:157:in `[]': can't convert C/Objective-C value
`0x8004676a0' of type `v' to Ruby object (ArgumentError)

I'm not sure what this means. I've played around with the Pointer
class and I can get this error a lot.

The original ObjC signature that is called to open the NSOpenPanel is:

- (void)beginSheetForDirectory:(NSString *)path file:(NSString *)name
modalForWindow:(NSWindow *)docWindow modalDelegate:(id)modalDelegate
didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo

Where the recipe (NSManagedObject_Recipe) is passed into a (void
*)contextInfo. Do I need to do anything fancy for this? Is the (void
*) causing the Pointer creation?

On Wed, Dec 31, 2008 at 1:46 PM, Laurent Sansonetti
<lsansone...@apple.com> wrote:
On Dec 30, 2008, at 7:32 PM, Nic Williams wrote:

In this code sample: http://gist.github.com/41855 I am getting a
Pointer instance instead of an NSManagedObject_Recipe instance (from
Core Data). Subsequently the setValue:forKey: call at the end of the
delegate/callback method fails.

The top method addImage(sender) launches an NSOpenPanel, which
delegates to the second/last method
addImageSheetDidEnd:returnCode:contextInfo: when the panel is closed.

But instead of passing through the NSManagedObject_Recipe instance to
the contextInfo: recipe value, I am getting a Pointer.

Is this correct? Or how do I get my NSManagedObject_Recipe instance
from the Pointer. MacRuby src for Pointer class doesn't suggest it has
any methods for getting the pointed-to thing.

 rb_cPointer = rb_define_class("Pointer", rb_cObject);
 rb_undef_alloc_func(rb_cPointer);
 rb_define_singleton_method(rb_cPointer, "new_with_type",
rb_pointer_new_with_type, 1);
 rb_define_method(rb_cPointer, "assign", rb_pointer_assign, 1);
 rb_define_method(rb_cPointer, "[]", rb_pointer_aref, 1);

You can do pointer[0] to dereference it.

A Pointer object can be created from a C array of elements, which is why Pointer responds to #[] (so that you can dereference a particular slot).

Laurent
_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel




--
Dr Nic Williams
iPhone and Rails consultants - http://mocra.com
Fun with iPhone/Ruby/Rails/Javascript - http://drnicwilliams.com
* Surf Report for iPhone - http://mocra.com/projects/surfreport/ *
_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

Reply via email to