Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Last active March 19, 2019 23:10
Show Gist options
  • Save NachoSoto/b3ffa949958aa8eb1432012e31004851 to your computer and use it in GitHub Desktop.
Save NachoSoto/b3ffa949958aa8eb1432012e31004851 to your computer and use it in GitHub Desktop.
Extensions to simplify usage of accessibilityTraits in Swift 4.2:
// Extensions to simplify usage of accessibilityTraits in Swift 4.2:
public func |= (traits: inout UIAccessibilityTraits, other: [UIAccessibilityTraits]) {
for o in other {
traits |= o
}
}
public func |= (traits: inout UIAccessibilityTraits, other: UIAccessibilityTraits) {
// From: https://swift.org/migration-guide-swift4.2/#known-migration-issues
traits = UIAccessibilityraits(rawValue: traits.rawValue | other.rawValue)
}
// Before Swift 4.2:
view.accessibilityTraits |= UIAccessibilityTraitButton
// In Swift 4.2:
view.accessibilityTraits = UIAccessibilityTraits(rawValue: view.accessibilityTraits.rawValue | UIAccessibilityTraits.header.rawValue)
// Example usage:
view.accessibilityTraits |= [.header, .link]
@PaulSolt
Copy link

PaulSolt commented Oct 4, 2018

Why did this change?

Was there a related Swift Evolution proposal? Or is this a known "bug" in Swift 4.2?

You may see errors like binary operator '|=' cannot be applied to two 'UIAccessibility.Traits' operands
Workaround: | the raw values of the LHS and RHS, pass the result to UIAccessibilityTraits(rawValue:) and assign to the LHS

@matt-curtis
Copy link

...What's wrong with formUnion(_:)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment