I was reading apple swift programming language and I didnt understand what is assert function and when we use it?
assert(condition: Bool, message: String)
I was reading apple swift programming language and I didnt understand what is assert function and when we use it?
assert(condition: Bool, message: String)
Assert provides a way for your code to reflect your assumptions explicitly, and check them when your code is running. Ideally, they are never triggered.
In an unlikely situation when an assertion is triggered, the message lets you know that your code has been modified in a way that breaks your assumption. This is very valuable, because it shrinks the area in which you search for newly introduced errors.
This is easier to understand with an example. Suppose that your code gets end-user input, and ensures that your end-user checks off at least one value in a list of check boxes. Another part of the code gets the list, and assumes that it is not empty. This is a good place to use an assertion: you assert that the list has non-zero size, and add a message explaining that your input form has validated the list before.