Objective c 在swift中,断言(0)无法将Int类型的值转换为预期的参数类型Bool
我正在将objective-C转换为swift,但在swift中写入断言(0)时出错,错误消息是“在swift中,断言(0)无法将Int类型的值转换为预期的参数类型Bool” 目标c中的我的代码:Objective c 在swift中,断言(0)无法将Int类型的值转换为预期的参数类型Bool,objective-c,swift,assert,Objective C,Swift,Assert,我正在将objective-C转换为swift,但在swift中写入断言(0)时出错,错误消息是“在swift中,断言(0)无法将Int类型的值转换为预期的参数类型Bool” 目标c中的我的代码: switch ([[UIApplication sharedApplication] applicationState]) { case UIApplicationStateActive: [statusStr appendString:@"foreground
switch ([[UIApplication sharedApplication] applicationState]) {
case UIApplicationStateActive:
[statusStr appendString:@"foreground"];
break;
case UIApplicationStateInactive:
[statusStr appendString:@"inactive"];
break;
case UIApplicationStateBackground:
[statusStr appendString:@"background"];
break;
default:
assert(0);
break;
}
并用swift翻译:
switch UIApplication.shared.applicationState {
case .active:
statusStr += "foreground"
case .inactive:
statusStr += "inactive"
case .background:
statusStr += "background"
default:
assert(0)
break
}
我不知道0在swift中的意思,让它assert(true)
或assert(false)
提前感谢。由于OC弱类型语言,它可以自动将0转换为false,因此在assert中,它可以运行,但swift是强类型语言,因此您应该使用assert(false)或自己翻译它关于错误消息,您不了解的是什么,它非常清楚。assert需要一个Bool,你给它一个Int。这不起作用。执行
assert(false)
,无论该执行什么操作。只需尝试assert(true)
和assert(false)
。。。哪一个触发?@MartinRassert(false)
will fireBtw.,fatalError()
可能是一个更好的选择,比较一下。实际上,Swift中根本不需要default
案例,因为开关是穷举的。你没有收到一个永远不会被执行的警告吗?