Jason Barker Jason Barker - 1 month ago 4x
Swift Question

How to remove member from OptionSet

I'm trying this in an iOS playground in Xcode 8 but it doesn't work:

struct Direction: OptionSet {
let rawValue: UInt8
static let none = Direction(rawValue: 0)
static let up = Direction(rawValue: 1 << 0)
static let left = Direction(rawValue: 1 << 1)
static let down = Direction(rawValue: 1 << 2)
static let right = Direction(rawValue: 1 << 3)
static let all = [up, left, down, right]

var directions = Direction.all
directions.remove(.up) // Error: Missing argument label 'at:' in call

Apple's documentation indicates that I should be able

"...to add or remove members from an instance of your custom option
set type."

The documentation refers to a
function but this isn't working. What am I doing wrong?


Try changing the declaration of all:

static let all: Direction = [.up, .left, .down, .right]