Binary operator '|' cannot be applied to operands at addOrRemoveFontTraitWithName

195 Views Asked by At

I am converting my Objective-c code to swift 2 and I am getting the following errors:

Binary operator '&' cannot be applied to operands of type 'UIFontDescriptorSymbolicTraits' and 'UInt32'

Binary operator '|' cannot be applied to operands of type 'UIFontDescriptorSymbolicTraits' and 'UInt32'

This is my code: errors at the 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)
    }
...
}

And i tried it with "traitValue: UIFontDescriptorSymbolicTraits" for the parameter "traitvalue"

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

Now the errors are

Binary operator '|' cannot be applied to two 'UIFontDescriptorSymbolicTraits' operands

Unary operator '~' cannot be applied to an operand of type 'UIFontDescriptorSymbolicTraits'

This error was already posted here, but no solution for me. It drives me crazy...:-(

1

There are 1 best solutions below

6
On

Use the rawValue which is UInt32

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