Swift -Ounchecked and Assertions

天涯浪子 提交于 2019-12-05 16:19:19

You should definitely not compile -Ounchecked for release in order to use precondition only while testing. Compiling -Ounchecked also disables checks for things like array out of bounds and unwrapping nil, that could lead to some pretty nasty production bugs involving memory corruption.

You can control assertion behaviour independently of compiler optimization settings via the -assert-config argument to swiftc:

$ cat assert.swift 
assert(false, "assertion asserted")
println("made it here...")
$ swiftc -Onone assert.swift; ./assert
assertion failed: assertion asserted: file assert.swift, line 1
Illegal instruction: 4
$ swiftc -O assert.swift; ./assert
made it here...
$ swiftc -O -assert-config Debug assert.swift; ./assert
assertion failed: assertion asserted: file assert.swift, line 1
Illegal instruction: 4
$ 

It doesn’t look like there’s an Xcode Build Settings toggle for this, but you can add using the “Other Swift Flags” setting.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!