[MacRuby-devel] Using a context with addObserver

Laurent Sansonetti lsansonetti at apple.com
Tue May 18 16:11:29 PDT 2010


Hi Dave,

I see. If this pattern is used inside the Apple Sketch sample (so, in source code you control and not in a framework), then indeed passing a unique Pointer instance should do the trick.

There are probably better ways to do what you want without using the context argument, once you are done with the conversion I can have a look and see if it cannot be written differently :)

In any case, we should allow strings and/or symbols to be passed as 'void *' arguments. I will try to implement that later.

Thanks for porting this sample! 

Laurent

On May 18, 2010, at 3:05 PM, Dave Baldwin wrote:

> Hi Laurent,
> 
> I am converting the Apple Sketch sample app to macruby and this construct is used everywhere.  My understanding (maybe faulty) is all they are trying to do is generate a unique pointer - pointing at a unique static string should do this, and by assigning it to a constant will show up compile errors if spelt wrong.
> 
> The context argument can just be a constant to allow the response to the observed behaviour to vary or it can be auxiliary information so this makes sense to me as you might need to know why, how or who caused something changed, rather than just the fact it has changed.
> 
>  In Ruby you would probably just use a symbol, but I presume this would throw up a run time error like a string does.
> 
> Dave.
> 
> 
> On 18 May 2010, at 22:36, Laurent Sansonetti wrote:
> 
>> Hi Dave,
>> 
>> Sorry for the late reply.
>> 
>> I'm afraid it's not currently possible to pass a string as an API that accepts a 'void *' (which seems to be your case). If you file a ticket on the tracker we can address this problem.
>> 
>> Your Pointer.new call may not raise a runtime exception but I suspect it's not going to behave correctly later.
>> 
>> I'm not familiar with the API you are using but it seems very uncommon to pass constants as context arguments to alter behaviors. Maybe there is another way to do what you want?
>> 
>> Laurent
>> 
>> On May 17, 2010, at 2:25 AM, Dave Baldwin wrote:
>> 
>>> Digging a bit more I think a solution to my own questions is:
>>> 
>>> On 13 May 2010, at 14:33, Dave Baldwin wrote:
>>> 
>>>> I am trying to translate code that looks like this:
>>>> 
>>>> static NSString *SKTWindowControllerCanvasSizeObservationContext = @"com.apple.SKTWindowController.canvasSize";
>>>> 
>>>> and later
>>>> 
>>>> [[self document] addObserver:self forKeyPath:SKTDocumentCanvasSizeKey options:NSKeyValueObservingOptionNew context:SKTWindowControllerCanvasSizeObservationContext];
>>>> 
>>>> to Ruby:
>>>> 
>>>> SKTWindowControllerCanvasSizeObservationContext = "com.apple.SKTWindowController.canvasSize"
>>> 
>>> SKTWindowControllerCanvasSizeObservationContext = Pointer.new(:char)
>>> 
>>> as all we are after is a unique value.  I still don't understand why the original version wasn't acceptable as the documentation says the context argument can be a C pointer or an object reference.  Anyhow, this has got me past this point but I have still to prove the final result when it is used by the observer.
>>> 
>>>> document.addObserver(self, forKeyPath: SKTDocumentCanvasSizeKey, 
>>>> 						options: NSKeyValueObservingOptionNew,
>>>> 						context:SKTWindowControllerCanvasSizeObservationContext)
>>>> 
>>>> but when it runs I get this error:
>>>> 
>>>>  expected instance of Pointer, got `"com.apple.SKTWindowController.canvasSize"' (String) (TypeError)
>>>> 
>>>> How do I get the a pointer to the context string?  All the examples I have found for the Pointer class seem to expect an objective C routing to fill it in.
>>>> 
>>>> Thanks,
>>>> 
>>>> Dave.
>>>> 
>>>> 
>>> 
>>> Dave.
>>> 
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100518/aedafbee/attachment-0001.html>


More information about the MacRuby-devel mailing list