Reputation: 467
import UIKit
var b = 3
assert(b <= 0, "this is impossible")
and Xcode shows:"exc_bad_instruction (code=exc_i386_invop subcode=0x0)", anyone has an idea?!, Is there in my Xcode problem? anyone knows where is the setting playground Optimization Level?
Upvotes: 0
Views: 99
Reputation: 2914
assert(@autoclosure condition: () -> Bool, @autoclosure _ message: () -> String = default, file: StaticString = #file, line: UInt = #line)
Traditional C-style assert with an optional message. Use this function for internal sanity checks that are active during testing but do not impact performance of shipping code. To check for invalid usage in Release builds; see
precondition
. * In playgrounds and -Onone builds (the default for Xcode's Debug configuration): ifcondition
evaluates to false, stop program execution in a debuggable state after printingmessage
. * In -O builds (the default for Xcode's Release configuration),condition
is not evaluated, and there are no effects. * In -Ounchecked builds,condition
is not evaluated, but the optimizer may assume that it would evaluate totrue
. Failure to satisfy that assumption in -Ounchecked builds is a serious programming error.
So your code is working as expected unless your variable b
assigned dynamically.
var b = 3
assert(b <= 0, "this is impossible") //It is expected here to stop the execution
//since your b is not less than or equal to 0 which results in true and hence
//`assert` will stop the execution.
Upvotes: 0
Reputation: 1595
On second thought, I think your code is working as it should. Assert should crash your app at runtime if the assertion fails. Check to see if your error message is printed to the console, if so it is working.
Upvotes: 1