我有一个SomeClass数组,它是各种其他类的超类.
该数组中包含所有这些随机类.
有没有办法使用switch(相反,如果让某些东西= elm为?TheSubClassType)
该数组中包含所有这些随机类.
有没有办法使用switch(相反,如果让某些东西= elm为?TheSubClassType)
在伪代码中:
- for AObjectOfTypeSomeClass in MyBigArray{
- switch the_type_of(AObjectOfTypeSomeClass){
- case SubClass1:
- let O = AObjectOfTypeSomeClass as! SubClass1
- ...
- ...
- ...
- }
- }
你很亲密
- for objectOfSomeClass in MyBigArray {
- switch objectOfSomeClass {
- case let subClass as SubClass1:
- // Do what you want with subClass
- default:
- // Object isn't the subclass do something else
- }
- }
这个网站有我发现的模式匹配的最佳纲要.
http://appventure.me/2015/08/20/swift-pattern-matching-in-detail/