2016-02-09 107 views
1

我想將幾個屬性附加到NSAttributeString並不斷收到以下錯誤。 Screenshot無法將類型NSRange的值轉換爲預期的參數類型

func strikeThroughStyle() {    
    let range = NSMakeRange(0, 1) 
    // add styles to self 
    self.attribute(self.string, atIndex: 0, effectiveRange: range) 
} 

我得到的錯誤:

Cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'NSRangePointer' (aka 'UnsafeMutablePointer<_NSRange>')

+0

這可能會與http://stackoverflow.com/questions/25825301/translate-nsrangepointer-from-objective-c-to-swift – Breek

回答

4

attribute:atIndex:effectiveRange:吸氣方法 - 它不設置/連接/添加屬性,將其報告給你的屬性值是在什麼字符串中的特定索引。因此,effectiveRange參數是一個「out-pointer」:您將指針傳遞給NSRange,並且該方法在返回時在該指針處填充數據。在斯威夫特(和NSAttributedString擴展中),你可以這樣調用:

var range = NSRange() 
let value = self.attribute(self.string, atIndex: 0, effectiveRange: &range) 

然而,這不是你想要的。你似乎想字符串上的一個屬性,而不是得到現有屬性的值。爲此,請使用NSMutableAttributedStringaddAttribute:value:range:方法,或者(尤其是如果您將屬性應用於整個字符串)NSAttributedStringinit(string:attributes:)構造函數。

相關問題