๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/Swift

(13)
[Swift] ๊ณ ์ฐจํ•จ์ˆ˜ - map, filter, reduce ์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค! ์˜ค๋Š˜์€ Swift์˜ ๊ณ ์ฐจํ•จ์ˆ˜ ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Swift์˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ณ ์ฐจํ•จ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ์ž์ฃผ ์“ฐ์ด๊ณ , ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์‹œ์—๋„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” map, filter, reduce ์š” ์„ธ๊ฐ€์ง€ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ณ ์ฐจํ•จ์ˆ˜๋ž€? ๊ณ ์ฐจํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. (Swift๋Š” ํ•จ์ˆ˜๋ฅผ ์ผ๊ธ‰ ๊ฐ์ฒด๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!) ๊ณ ์ฐจํ•จ์ˆ˜์˜ ์ข…๋ฅ˜๋กœ๋Š” map, filter, reduce, forEach, compactMap, flatMap ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” map, filter, reduce์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ด…์‹œ๋‹ค..
[Swift] ํƒ€์ž… ์ถ”๋ก ๊ณผ ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜ ์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค! ๊ธ€์„ ๊ต‰์žฅํžˆ ์˜ค๋žœ๋งŒ์— ์˜ฌ๋ฆฌ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ,, ๊ทธ๋™์•ˆ TIL ์Šคํ„ฐ๋””๋ฅผ ํ•˜๋ฉฐ ๊นƒํ—™์— ๊ทธ๋‚  ๊ณต๋ถ€ํ•œ๊ฑธ ์—…๋กœ๋“œํ•˜๋‹ค๋ณด๋‹ˆ ํ‹ฐ์Šคํ† ๋ฆฌ ์—…๋กœ๋“œ๋ฅผ ๊ฑฐ์˜ ๊นŒ๋จน๊ฒŒ๋œ... ๋จธ ๊ทธ๋Ÿฐ ์‚ฌ์—ฐ์ด...์žˆ์ง€๋งŒ ์‚ฌ์‹ค ํ•‘๊ณ„๊ณ ..ใ…Ž TIL ๊ธ€์„ ํ‹ฐ์Šคํ† ๋ฆฌ์—๋„ ์˜ฎ๊ธฐ๋ฉด์„œ ์ด์ œ ๋‹ค์‹œ ๊พธ์ค€ํžˆ ์—…๋กœ๋“œ๋ฅผ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!! ์•„์ขŒ์ž~ ์˜ค๋Š˜์€ Swift์—์„œ ์ž๋ฃŒ๋ฅผ ๋ช…์‹œํ•˜๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ธ ํƒ€์ž… ์ถ”๋ก ๊ณผ ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์„ ๋น„๊ตํ•˜๋ฉฐ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! Swift์—์„œ์˜ ์ƒ์ˆ˜, ๋ณ€์ˆ˜ ์„ ์–ธ let : ์ƒ์ˆ˜ ์„ ์–ธ ํ‚ค์›Œ๋“œ var : ๋ณ€์ˆ˜ ์„ ์–ธ ํ‚ค์›Œ๋“œ let ์ƒ์ˆ˜๋ช…: ํƒ€์ž… = ๊ฐ’ var ๋ณ€์ˆ˜๋ช…: ํƒ€์ž… = ๊ฐ’ // ์„ ์–ธ ์˜ˆ์‹œ let name: String = "Eunju" var age: Int = 20 ์œ„์™€ ๊ฐ™์ด let๊ณผ var ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ƒ์ˆ˜๋‚˜ ..
[Swift] if let๊ณผ guard let์˜ ์ฐจ์ด ์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค!๐Ÿ˜Š ์˜ค๋Š˜์€ if let๊ณผ guard let์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Optional ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ†ตํ•ด ์•ˆ์ „ํ•˜๊ฒŒ ๊บผ๋‚ด์„œ ์“ฐ๋Š”๊ฒŒ ์ •๋ง ์ค‘์š”ํ•œ๋ฐ์š”! ํ‰์†Œ์— ์ •๋ง ๋ฌด์˜์‹์ ์œผ๋กœ if let๊ณผ guard let์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์ฐธ์—ฌ์ค‘์ธ ํ”„๋กœ์ ํŠธ์—์„œ ํŒ€์›์ด ์ด์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ๋‚จ๊ฒจ์ฃผ์–ด์„œ ๋‘๊ฐœ์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ํ™•์‹คํžˆ ์ •๋ฆฌํ•˜๊ณ  ๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๊ณตํ†ต์  ๋จผ์ €, ๋‘๊ฐœ์˜ ๊ณตํ†ต์ ์— ๋Œ€ํ•ด ์งš๊ณ  ๋„˜์–ด๊ฐˆ๊ฒŒ์š”! if let๊ณผ guard let์€ ๊ณตํ†ต์ ์œผ๋กœ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์ด๋ผ๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…”๋„(Optional)์€ ๊ฐ’์ด ์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ๋“ฑ์— ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†์Œ, ๋‹ค์‹œ ๋งํ•ด ๋ณ€์ˆ˜ ๋˜..
[Swift] Escaping Closure(ํƒˆ์ถœ ํด๋กœ์ €) ์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ํด๋กœ์ €(Closure)์˜ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ •๋ฆฌํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Escaping Closure์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!๐Ÿ˜Š ํด๋กœ์ €์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด์‹œ๊ฒ ๋‹ค๋ฉด ์ง€๋‚œ ํฌ์ŠคํŒ…์„ ๋ณด๊ณ  ์˜ค์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค! [Swift] ํด๋กœ์ €(Closure) ์ตœ๊ทผ์— iOS ์Šคํ„ฐ๋”” ์นœ๊ตฌ๋“ค๊ณผ ํ•จ๊ป˜ ํด๋กœ์ €์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์•˜๋Š”๋ฐ์š”, ๋ธ”๋กœ๊ทธ์—๋„ ๊ทธ ๋‚ด์šฉ์„ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!๐Ÿ˜Š ๋ฐ์ดํ„ฐ ์ „๋‹ฌ, ์„œ๋ฒ„ ํ†ต์‹  ๋“ฑ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์ด์šฉ๊ฐ€๋Šฅํ•œ ํด๋กœ janechoi.tistory.com Escaping Closure๋ž€? escaping closure๋Š” ํ•ด์„ ๊ทธ๋Œ€๋กœ ํƒˆ์ถœ ํด๋กœ์ €๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ์š”, ๋ง ๊ทธ๋Œ€๋กœ 'ํƒˆ์ถœ ๊ฐ€๋Šฅํ•œ' ํด๋กœ์ €๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒˆ์ถœ..? ์–ด๋””๋ฅผ ํƒˆ์ถœํ•ด...? ๋ผ๋Š” ์˜๋ฌธ์ด ๋“œ์‹ค..
[Swift] ํด๋กœ์ €(Closure) ์ตœ๊ทผ์— iOS ์Šคํ„ฐ๋”” ์นœ๊ตฌ๋“ค๊ณผ ํ•จ๊ป˜ ํด๋กœ์ €์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์•˜๋Š”๋ฐ์š”, ๋ธ”๋กœ๊ทธ์—๋„ ๊ทธ ๋‚ด์šฉ์„ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!๐Ÿ˜Š ๋ฐ์ดํ„ฐ ์ „๋‹ฌ, ์„œ๋ฒ„ ํ†ต์‹  ๋“ฑ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์ด์šฉ๊ฐ€๋Šฅํ•œ ํด๋กœ์ €์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค!! ํด๋กœ์ €(Closure)๋ž€? ํด๋กœ์ €๋Š” ์ผ์ •๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๋ธ”๋ก์œผ๋กœ ๋ชจ์•„๋†“์€ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์‹คํ–‰๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ธ”๋Ÿญ์ด๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋„ ํด๋กœ์ €์˜ ํ•œ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. (ํ•จ์ˆ˜๋Š” ์ด๋ฆ„์ด ์žˆ๋Š” ํด๋กœ์ €) ํด๋กœ์ €๋ผ๋Š” ๊ฐœ๋… ์•ˆ์— ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. → ํ•จ์ˆ˜: Named Closure, ์ด์™ธ์˜ ์ผ๋ฐ˜์ ์ธ ํด๋กœ์ €: (Unnamed) Closure ํด๋กœ์ €๋Š” ์ผ๊ธ‰๊ฐ์ฒด์˜ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ๋Œ€์ž…ํ•˜๊ณ , ๋ฐ˜ํ™˜(return) ๊ฐ€๋Šฅํ•˜๋ฉฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ..