相关疑难解决方法(0)

来自Swift Range的NSRange?

问题:当我使用使用Range的Swift String时,NSAttributedString接受NSRange

let text = "Long paragraph saying something goes here!"
let textRange = text.startIndex..<text.endIndex
let attributedString = NSMutableAttributedString(string: text)

text.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in

    if (substring == "saying") {
        attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange)
    }
})
Run Code Online (Sandbox Code Playgroud)

产生以下错误:

错误:'Range'不能转换为'NSRange'aligolsString.addAttribute(NSForegroundColorAttributeName,value:NSColor.redColor(),range:substringRange)

macos ios nsrange swift

153
推荐指数
8
解决办法
11万
查看次数

标签 统计

ios ×1

macos ×1

nsrange ×1

swift ×1