Featured image of post assert 알아보기

assert 알아보기

Assertion

assert(_:_:file:line:) 함수를 사용한다.
assert 함수는 디버깅 모드에서만 동작하고 배포하는 어플리케이션에서는 제외된다. 조건 체크, 테스트 등에 사용한다.

Declaration

총 4개의 인자를 받을 수 있다.
체크할 조건문 / 조건 미충족 시 발생하는 오류 메시지 / 메세지를 출력할 파일 / 출력할 메세지 라인 수

1
2
3
4
5
6
func assert(
    _ condition: @autoclosure () -> Bool,
    _ message: @autoclosure () -> String = String(),
    file: StaticString = #file,
    line: UInt = #line
)

📖 Parameters

- condition
  The condition to test. condition is only evaluated in playgrounds and -Onone builds.

- message
  A string to print if condition is evaluated to false. The default is an empty string.

- file
  The file name to print with message if the assertion fails. The default is the file where assert(_:_:file:line:) is called.

- line
  The line number to print along with message if the assertion fails. The default is the line number where assert(_:_:file:line:) is called.

Usage

1
2
3
4
5
6
var someInt: Int = 0

assert(someInt == 0, "someInt != 0")

someInt = 1
assert(someInt == 0, "someInt != 0") // main.swift:15: Assertion failed: someInt != 0

Reference

Apple Developer - assert

yagom youtube - assert와 guard

Licensed under CC BY-NC-SA 4.0