๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS ๐ŸŽ/Swift

Swift15 - Nil-Coalescing Operator

by yongmin.Lee 2021. 2. 3.

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