-
assert
:传统的C样式断言。
assert
主要作用是在测试期间进行内部健全性检查,在release状态下是不运行。
func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line)
参数:
condition:要测试的条件。condition仅在playgrounds和debug中进行评估。如果condition求值为false,则在打印后停止以可调试状态执行程序message。
message:一个字符串打印如果condition被评估false。默认值为空字符串。
file:message如果断言失败,则打印文件名。默认值是调用的文件。assert(_:_:file:line:)
line:要打印的行号以及message断言是否失败。默认值是调用的行号。assert(_:_:file:line:)
-
assertionFailure
:表示内部完整性检查失败。
assertionFailure
使用此功能能让App停止运行,说白了可以制造崩溃。同样在debug执行,在release下不执行。
func assertionFailure(_ message: @autoclosure () -> String = String(),file: StaticString = #file, line: UInt = #line)
参数:
message:在playgrounds和debug中打印的字符串。默认值为空字符串。
file:要打印的文件名message。默认值是调用的文件。assertionFailure(_:file:line:)
line:要打印的行号message。默认值是调用的行号。assertionFailure(_:file:line:)
-
precondition
:必须要满足条件,才会执行后面的。
precondition
与assert
类似,只是precondition
不管是在debug还是release都会执行,在debug下会打印输出。
func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line:UInt = #line)
参数
condition:要测试的条件。
message:condition要false在playgrounds和debug中进行评估的字符串。默认值为空字符串。
file:message如果前提条件失败,则打印的文件名。默认值是调用的文件。
line:要打印的行号以及message断言是否失败。默认值是调用的行号。
-
preconditionFailure
:表示违反了前提条件,直接终止App。
preconditionFailure
与assertionFailure
类似,只是precondition
不管是在debug还是release都会执行,在debug下会打印输出。
func preconditionFailure(_ message: @autoclosure () -> String = String(),file: StaticString = #file, line: UInt = #line) -> Never
参数
message:在操场或-Onone构建中打印的字符串。默认值为空字符串。
file:要打印的文件名message。默认值是调用的文件。
line:要打印的行号message。默认值是调用的行号。
-
fatalError
:无条件地打印给定的消息并停止执行。
func fatalError(_ message: @autoclosure () -> String = String(), file:StaticString = #file, line: UInt = #line) -> Never
参数
message:要打印的字符串。默认值为空字符串。
file:要打印的文件名message。默认值是调用的文件。
line:要打印的行号message。默认值是调用的行号。