cmarti1138 cmarti1138 - 5 months ago 12
Swift Question

Why am I getting an argument type reject with the following code in swift and optionSetType structure values?

struct TimeMark: OptionSetType {

private enum TimeMark : Int,CustomStringConvertible {
case Header=0, Current=1

var description : String {
var shift = 0
while (rawValue >> shift != 1){ shift += 1 }
return ["Header", "Current"][shift]
}
}

let rawValue: Int

internal init(rawValue: Int) {
self.rawValue = rawValue
}
private init(_ timeMark: TimeMark) { self.rawValue = timeMark.rawValue
}

static let Header = 0
static let Current = 1

}

let mark: TimeMark

let rounded: Bool = mark == TimeMark.Current


I am getting the reject with the last line.

enter image description here

I also changed my comparison operator to ===(which I didn't think would work) and that didn't work as well.

Answer

You should initialize it before using or accessing it's properties. Try this:

let mark = TimeMark()
let rounded = (mark.rawValue == TimeMark.Current)
Comments