Question

I am trying to implement the following method of NSAttributedString in Macruby:

  • (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange

As by definition, it Returns the value for an attribute with a given name of the character at a given index, and by reference the range over which the attribute applies.

OK, so I need a pointer to NSRange, which I set up as follows:

range=Pointer.new("{_NSRange=QQ}")[0]

It seems to be fine as range.class => NSRange.

However, when I execute the method:

font=txtStor.attribute(NSFontAttributeName,atIndex:index,effectiveRange:range)

my range is always #<NSRange location=0 length=0>. Also, p range gives me #<NSRange location=0 length=0>.

Any ideas how to implement this correctly?

Was it helpful?

Solution

I was given the solution by Watson at the MacRuby-devel mailing list. I should have written my code like that:

 range=Pointer.new(NSRange.type)
 #though range=Pointer.new("{_NSRange=QQ}") would also work

then, as I had before

font=txtStor.attribute(NSFontAttributeName,atIndex:index,effectiveRange:range)

and if needed I could dereference the range by range[0].

OTHER TIPS

This is another valid solution

 NSRange range;
 font=txtStor.attribute...effectiveRange:&range);

Here the range is passed by reference, in other words the address of the value of range is passed, this allows the value to be changed inside the method and that have that changed remain in the caller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top