Nil-Coalescing Operator
The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.
nil ๋ณํฉ ์ฐ์ฐ์๋
- Optional ?? Value
- ์ต์ ๋ ๊ฐ์ด nil์ด ์๋๋ฉด ํด๋น ๊ฐ ๋ฐํ, ์ต์ ๋ ๊ฐ์ด nil์ด๋ฉด ์ค๋ฅธ์ชฝ ๊ฐ ๋ฐํ
var optionalVal1: String? = nil
var optionalVal2: String? = "hello"
var res1 = optionalVal1 ?? "Nil-Coalescing"
var res2 = optionalVal2 ?? "Operator"
print(res1) // "Nil-Coalescing"
print(res2) // "hello"
'iOS ๐ > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift17 - assert/guard (0) | 2021.02.10 |
---|---|
Swift16 - ํ์ ์บ์คํ (0) | 2021.02.10 |
Swift14 - Optional Chaining (0) | 2021.02.03 |
Swift13 - Initialization & Deinitialization (0) | 2021.02.02 |
Swift12 - ์์ (0) | 2021.02.02 |