我无法绕过这个错误。当我运行这个函数时,我得到一个致命的错误。致命错误:意外发现零而展开的可选值低于 的功能是:意外地发现零,而从NSMutableString解包可选值
func changeSelectedFontColor(fontColor: UIColor) {
let selectedRange : NSRange = self.textView.selectedRange
var currentAttributesDict : NSDictionary = textView.textStorage.attributesAtIndex(selectedRange.location, effectiveRange: nil)
var currentFont : UIFont = currentAttributesDict.objectForKey(NSFontAttributeName) as UIFont
let italFont = [NSFontAttributeName:UIFont(name: "Georgia-Italic", size: 18.0)]
var currentColor : UIColor = currentAttributesDict.objectForKey(NSForegroundColorAttributeName) as UIColor
var currentBGColor : UIColor = currentAttributesDict.objectForKey(NSBackgroundColorAttributeName) as UIColor
var currentUnderlinedText : UIFont = currentAttributesDict.objectForKey(NSUnderlineStyleAttributeName) as UIFont
var currentparagraphStyle : NSMutableParagraphStyle = currentAttributesDict.objectForKey(NSParagraphStyleAttributeName) as NSMutableParagraphStyle
}
我正在构建一个文本编辑器,并使用这些变量来检查是否已经对所选文本进行了一些格式更改。如果有的话,我可以将它们添加到当前的更改中。例如,可能选中的文本带下划线,我稍后想要更改文本的颜色。我需要检查下划线属性是否已设置,然后将其添加到属性字符串以及新更改,以便所有更改都会保留。我希望我解释得很好。 – LearningGuy 2014-11-07 14:10:30
在这种情况下,对于每个变量,您应该使用[可选绑定](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097 -CH5-XID_503)来检查一个值是否不为零,并根据需要做任何事情 – Antonio 2014-11-07 14:32:34