使用 Guard 捕获除一个之外的所有枚举状态
使用后卫击中障碍物。理想情况下,想要执行一个保护语句来捕获除一个之外的所有枚举状态,因此会类似于:
guard case .notInUse != foundTransition.validToObject.isInSituation else {
fatalError("Transition: The toObject is already in a situation")
}
但似乎不允许这种不匹配的测试。因此,请使用下面的 if 语句:
if case .notInUse = foundTransition.validToObject.isInSituation {} else {
fatalError("Transition: The toObject is already in a situation")
}
它可以工作,但感觉守卫会更整洁。有什么想法吗?
Hitting a block on using a guard. Ideally want to do a guard statement that traps all enum states other than one, so would be something like:
guard case .notInUse != foundTransition.validToObject.isInSituation else {
fatalError("Transition: The toObject is already in a situation")
}
But this non matching test does not seem to be allowed. So instead using the below if statement:
if case .notInUse = foundTransition.validToObject.isInSituation {} else {
fatalError("Transition: The toObject is already in a situation")
}
It works but feels a guard would be neater. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可能否定
case
语句。您要么需要使用
if
语句,要么进行枚举Equatable
,在这种情况下,您只需删除 case 关键字即可。或者,您可以使用由
switch
或if
支持的guard
语句。但你永远摆脱不了它们!It is impossible to negate a
case
statement.You either need to use your
if
statement, or make the enumerationEquatable
, in which case you would just drop the case keyword.Alternatively, you can use a
guard
statement that is backed by aswitch
orif
. But you'll never get rid of them! ????