[swift-users] Switch based on let
Nate Birkholz
nbirkholz at gmail.com
Fri Jul 8 10:11:04 CDT 2016
This looks like it doesn't work (swift 2.x), but wanted to be sure it's not
supported:
class Superclass {}
class Subclass1 : Superclass {}
class Subclass2 : Superclass {}
class Subclass3 : Superclass {}
let sc1 = Subclass1()
let sc2 = Subclass2()
let sc3 = Subclass3()
let objects : [Superclass] = [sc1, sc2, sc3]
for subclassObject in objects {
switch subclassObject {
case let object = subclassObject as? Subclass1:
doSomethingWith(object)
case let object = subclassObject as? Subclass2:
doSomethingWith(object)
case let object = subclassObject as? Subclass3:
doSomethingWith(object)
default:
return
}
}
This gives an error, expecting a colon (:) after object on every case.
I wanted to be sure I wasn't missing something in my syntax (nor some
obvious-to-others reason this isn't supported) before going to swift
evolution.
--
Nate Birkholz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160708/d112ef2d/attachment.html>
More information about the swift-users
mailing list