二元运算符' |'不能应用于addOrRemoveFontTraitWithName的操作数

时间:2016-03-11 20:10:43

标签: xcode swift2 uifontdescriptor

我正在将Objective-c代码转换为swift 2,我收到以下错误:

二元运算符'&'不能应用于类型' UIFontDescriptorSymbolicTraits'的操作数。和' UInt32'

二元运算符' |'不能应用于类型' UIFontDescriptorSymbolicTraits'的操作数。和' UInt32'

这是我的代码:if {... else {

的错误
func addOrRemoveFontTraitWithName(traitName: String, andValue traitValue: UInt32, ...) {

    var currentAttributesDict : NSDictionary = self.textView.textStorage.attributesAtIndex(selectedRange.location, effectiveRange: nil)
    var currentFont : UIFont = currentAttributesDict .objectForKey(NSFontAttributeName) as! UIFont

    var fontDescriptor : UIFontDescriptor = currentFont.fontDescriptor()
    var fontNameAttribute : NSString = fontDescriptor.objectForKey(UIFontDescriptorNameAttribute) as! NSString
    var changedFontDescriptor : UIFontDescriptor

    if (fontNameAttribute.rangeOfString(traitName).location == NSNotFound){
        var existingTraitsWithNewTrait : UIFontDescriptorSymbolicTraits = fontDescriptor.symbolicTraits | traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    } else {
        var existingTraitsWithNewTrait : UIFontDescriptorSymbolicTraits = fontDescriptor.symbolicTraits & ~traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    }
...
}

我尝试使用" traitValue:UIFontDescriptorSymbolicTraits"参数" traitvalue"

    func addOrRemoveFontTraitWithName(traitName: String, andValue traitValue: UIFontDescriptorSymbolicTraits, ...)

现在错误是

二元运算符' |'不能应用于两个' UIFontDescriptorSymbolicTraits'操作数

一元运算符'〜'不能应用于类型' UIFontDescriptorSymbolicTraits'

的操作数

此错误已在此处发布,但对我来说无法解决。它让我疯狂......: - (

1 个答案:

答案 0 :(得分:2)

使用rawValue是UInt32

var existingTraitsWithNewTrait =
UIFontDescriptorSymbolicTraits(rawValue: fontDescriptor.symbolicTraits.rawValue | traitValue)