In the "What's New in Swift" video, the following pattern is shown on a slide:
for case .MyEnumCase (let value) in enumValues
But tests in Xcode 7 show that this sort of thing won't compile (with suitable declaration of a MyEnum that has a .MyEnumCase with associated value, and setting enumValues to a [MyEnum] value. The simpler syntax:
for case .MyEnumCase in enumValues
does compile, but of course doesn't give access to the associated value. Anyone know if this is merely a compiler defect, or if the actual syntax is something different?
Apparently the "let" have to be outside the parens, following "case", like this:
enum SomeEnum {
case Uppercase(String)
case Lowercase(String)
case Suitcases(Int)
}
let enumValues: [SomeEnum] = [.Uppercase("A"), .Lowercase("b"), .Suitcases(100), .Uppercase("FOO")]
for case let .Uppercase(value) in enumValues {
print(value)
}
// Prints
// A
// FOO
I can't see anything mentioned about this particular kind of "for case" (extracting associated values) in the iBook either.