Search code examples
objective-cswiftassert

in swift assert(0) Cannot convert value of type Int to expected argument type Bool


I am translating objective -C to swift, but I get error when I write assert(0) in swift, the error message is "in swift assert(0) Cannot convert value of type Int to expected argument type Bool "

my code in 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;
    }

and translate in swift :

 switch UIApplication.shared.applicationState {

            case .active:
                statusStr += "foreground"
            case .inactive:
                statusStr += "inactive"
            case .background:
                statusStr += "background"
            default:
                assert(0)
                break

            }

I don't know 0 mean in swift make it assert(true) or assert(false).

Thanks in advance.


Solution

  • Beacuse OC weak typing language, it can auto translate 0 to false, so in assert, it can be run, but swift is strong typing language, so you should use assert(false) or you should translate it by yourself