0

我正在尝试在 Macruby 中实现以下 NSAttributedString 方法:

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

根据定义,它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.

好的,所以我需要一个指向 NSRange 的指针,我设置如下:

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

似乎很好range.class=> NSRange

但是,当我执行该方法时:

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

range的总是#<NSRange location=0 length=0>。另外,p range给我#<NSRange location=0 length=0>.

任何想法如何正确实施?

4

2 回答 2

0

Watson 在 MacRuby-devel 邮件列表中为我提供了解决方案。我应该这样写我的代码:

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

然后,就像我以前一样

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

如果需要,我可以取消引用范围range[0]

于 2013-01-22T21:53:26.347 回答
0

这是另一个有效的解决方案

 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.

于 2014-10-21T13:36:48.753 回答