2013-01-21 37 views
0

我試圖實現NSAttributedString中的MacRuby以下方法返回值:MacRuby的指針和對象 - 功能通過引用

  • (ID)屬性:(的NSString *)的attributeName atIndex:(NSUInteger)指數effectiveRange:(NSRangePointer)人氣指數

作爲由定義,它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>

任何想法如何正確實施?

回答

0

我在MacRuby-devel郵件列表中獲得了Watson的解決方案。我應該寫我的代碼這樣的:

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

然後,我

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

有過,如果需要,我可以通過解引用的range[0]範圍。

0

這是另一種有效的解決方案

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

這裏的範圍通過引用傳遞,在範圍中的值的地址被傳遞換言之,這允許方法內要被改變的值,並且具有那個改變留在呼叫者。