S At
S At

Reputation: 467

What's was wrong in my simple code?

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

Answers (2)

Santosh
Santosh

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): if condition evaluates to false, stop program execution in a debuggable state after printing message. * 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 to true. 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

Ike10
Ike10

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

Related Questions