assert란?
- 특정 조건을 체크하고, 조건이 성립되지 않으면 메세지를 출력하게 할수 있는 함수
- assert 함수는 디버깅 모드에서만 동작하고 주로 디버깅 중 조건의 검증을 위하여 사용합니다.
guard 문이란?
- 뭔가를 검사하여 그 다음에 오는 코드를 실행할지 말지 결정하는 것
- guard 문에 주어진 조건문이 거짓일 때 구문이 실행됨
var value = 0
assert(value == 0)
value = 2
assert(value == 0, "값이 0이 아닙니다")
-> __lldb_expr_10/MyPlayground.playground:7: Assertion failed: 값이 0이 아닙니다
Playground execution failed:
error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18e02190c).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18e02190c)
* frame #0: 0x000000018e02190c libswiftCore.dylib`Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 1472
frame #1: 0x0000000106b48534 $__lldb_expr11`main at playground10-683c0e..swift:0
frame #2: 0x00000001043731f8 MyPlayground`linkResources + 300
frame #3: 0x000000018036607c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
frame #4: 0x0000000180365320 CoreFoundation`__CFRunLoopDoBlocks + 404
frame #5: 0x000000018035fbc8 CoreFoundation`__CFRunLoopRun + 764
frame #6: 0x000000018035f3bc CoreFoundation`CFRunLoopRunSpecific + 572
frame #7: 0x000000018afdd70c GraphicsServices`GSEventRunModal + 160
frame #8: 0x00000001843f03d0 UIKitCore`-[UIApplication _run] + 964
frame #9: 0x00000001843f51ac UIKitCore`UIApplicationMain + 112
frame #10: 0x00000001043732c0 MyPlayground`main + 200
frame #11: 0x0000000180224554 libdyld.dylib`start + 4
assert(value == 2, "값이 0이 아닙니다") //value가 2로
/*
guard 조건 else {
//조건이 false 이면 else 구문이 실행되고
return or throw or break 를 통해 이 후 코드를 실행하지 않도록 한다.
}
*/
func guardTest(value: Int?) {
guard let value == value else { return }
print(value) //"2\n"
}
guardTest(value: 2)
guardTest(value: nil)
'개발 > Ios(Swift)' 카테고리의 다른 글
IOS 스위프트(Swift) 문법 - 익스텐션 (0) | 2021.08.25 |
---|---|
IOS 스위프트(Swift) 문법 - 프로토콜 (0) | 2021.08.25 |
IOS 스위프트(Swift) 문법 - 타입캐스팅 (0) | 2021.08.19 |
IOS 스위프트(Swift) 문법 - 상속 (0) | 2021.08.19 |
IOS 스위프트(Swift) 문법 - 클래스와 구조체의 차이 (0) | 2021.08.19 |