swift – 在switch语句中使用if let语法
发布时间:2020-12-14 05:21:27 所属栏目:百科 来源:网络整理
导读:在 swift中,如果变量属于某种类型,if let语法只允许我执行语句.例如 class A {...}class B: A {...}class C: A {...}func foo(thing: A) { if let b = thing as? B { // do something }else if let c = thing as? C { // do something else }} 是否可以通过s
在
swift中,如果变量属于某种类型,if let语法只允许我执行语句.例如
class A {...} class B: A {...} class C: A {...} func foo(thing: A) { if let b = thing as? B { // do something }else if let c = thing as? C { // do something else } } 是否可以通过switch语句实现这一点? 我有这么远,但变量b和c仍然是A类型,而不是转换为B和C: func foo(thing: A) { switch thing { case let b where b is B: // do something case let c where c is C: // do something else default: // do something else: } }
如果你只想知道它是B还是C,你可以说案例是B而案例是C.
如果你想捕获并抛弃,那么说case b让b为B,case为c. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |