gpt4 book ai didi

objective-c - 在 swift assert(0) 中无法将 Int 类型的值转换为预期的参数类型 Bool

转载 作者:行者123 更新时间:2023-11-30 12:27:22 24 4
gpt4 key购买 nike

我正在将 Objective -C 翻译为 swift,但是当我在 swift 中编写断言(0)时出现错误,错误消息是“在 swift 断言(0)中无法将类型 Int 的值转换为预期参数类型 Bool ”

我在 objective-c 中的代码:

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;
}

并快速翻译:

 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)或者你应该自己翻译它

关于objective-c - 在 swift assert(0) 中无法将 Int 类型的值转换为预期的参数类型 Bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44019678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com