Search code examples
swiftassert

Assert and Precondition parameters in swift


As per the definition in apple documentation assert and precondition has the following signatures:

assert(_:_:file:line:) 
precondition(_:_:file:line:)

But while programming I did not find a way to use file and line parameters. Why are they actually used? Though the condition is failed I did not find any file or line number in the console.


Solution

  • The two parameters are defaulted by swift compiler, so you do not need to supply any values for them. In fact, you shouldn't supply them to avoid incorrect reporting.

    Below is an example of how a precondition failure is reported:

    Precondition failure

    Note how the file name MyPlayground.playground and line number 5 are filled in automatically.