๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail
[Swift Language Guide] ํด๋กœ์ € (Closures)
Swift Language Guide 2023. 6. 5. 20:40

ํด๋กœ์ € (Closures) ์ „์—ญํ•จ์ˆ˜ = ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์–ด๋– ํ•œ ๊ฐ’๋„ ์บก์ฒ˜ํ•˜์ง€ ์•Š๋Š” ํด๋กœ์ € ์ค‘์ฒฉ ํ•จ์ˆ˜ = ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ๋‘˜๋Ÿฌ์‹ผ ํ•จ์ˆ˜๋กœ ๋ถ€ํ„ฐ ๊ฐ’์„ ์บก์ฒ˜ํ•  ์ˆ˜ ์žˆ๋Š” ํด๋กœ์ € ์ฃผ๋ณ€ ์ปจํ…์ŠคํŠธ์—์„œ ๊ฐ’์„ ์บก์ฒ˜ํ•  ์ˆ˜ ์žˆ๋Š” ์š”์•ฝ๋œ ๊ตฌ๋ฌธ = ์ด๋ฆ„์ด ์—†๋Š” ํด๋กœ์ €(Unnamed Closure) ํด๋กœ์ €์˜ ํŒŒ๋ผ๋ฏธํ„ฐ → inout ํŒŒ๋ผ๋ฏธํ„ฐ ์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค. let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] func backward(_ s1: String, _ s2: String) -> Bool { return s1 > s2 } var reversedNames = names.sorted(by: backward) // backwardํ•จ์ˆ˜๋ฅผ ํด๋กœ์ €๋กœ ํ‘œํ˜„ rever..

article thumbnail
[Swift Language Guide] ํ•จ์ˆ˜(Functions)
Swift Language Guide 2023. 5. 24. 19:12

Functions With an Implicit Return func greeting(for person: String) -> String { "Hello, " + person + "!" } ์ „์ฒด ๋ณธ๋ฌธ์ด ํ•œ์ค„๋กœ ํ‘œํ˜„์ด ๋œ๋‹ค๋ฉด returnํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ Specifying Argument Labels func greet(person: String, from hometown: String) -> String { return "Hello \\(person)! Glad you could visit from \\(hometown)." } print(greet(person: "Bill", from: "Cupertino")) ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ธ์ž ์ด๋ฆ„ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ€๋ณ€ ํŒŒ๋ผ๋ฏธํ„ฐ (Variadic Para..

article thumbnail
[Swift Language Guide] ์ œ์–ดํ๋ฆ„(Control Flow)
Swift Language Guide 2023. 5. 24. 19:11

for๋ฌธ์˜ stride ๋ฉ”์†Œ๋“œ for i in stride(from: 0, to: 10, by: 2) { print(i) // 0, 2, 4, 6, 8 } for i in stride(from: 0, through: 10, by: 2) { print(i) // 0, 2, 4, 6, 8, 10 } // reverseํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. for i in stride(from: 3, through: 0, by: -1) { print(i) // 3, 2, 1, 0 } 1์”ฉ ์ฆ๊ฐ€๊ฐ€ ์•„๋‹Œ ์›ํ•˜๋Š” ์ˆซ์ž๋งŒํผ ์ฆ๊ฐ€ ์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ to : toํŒŒ๋ผ๋ฏธํ„ฐ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ์ œ์™ธํ•œ๋‹ค. through: throughํŒŒ๋ผ๋ฏธํ„ฐ์— ์žˆ๋Š” ์ˆ˜ ํฌํ•จํ•œ๋‹ค. repeat while ๊ตฌ๋ฌธ var num = 1 repeat { num +..

article thumbnail
[Swift Language Guide] ์ฝœ๋ ‰์…˜ ํƒ€์ž…(Collection Types)
Swift Language Guide 2023. 5. 24. 19:08

์ฝœ๋ ‰์…˜ ํƒ€์ž…(Collection Types) ๋ฐฐ์—ด, ์„ธํŠธ(์ง‘ํ•ฉ), ๋”•์…”๋„ˆ๋ฆฌ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ €์žฅํ•˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜๋‹ค. ex) Intํƒ€์ž…์˜ ๋ฐฐ์—ด์ด๋ฉด Int๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด(Array) var arr1: [Int] = [] // ์งง์€ ํ‘œํ˜„ ์„ ํ˜ธ var arr2: Array = [] var map = Array(repeating: Array(repeating: 0, count: 3), count: 2) // [[0, 0, 0], [0, 0, 0]] // 0 0 0 // 0 0 0 ๋ฐฐ์—ด์˜ ์„ ์–ธ ๋ฐฉ๋ฒ•: [Element], Array ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ๊ณ ์ •ํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ์ดˆ๊ธฐํ™”๋„ ๊ฐ€๋Šฅ ex) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€ ๋•Œ map ๋งŒ๋“ค ๋•Œ ์ž์ฃผ ์“ฐ๊ฒŒ ๋œ๋‹ค. var shoppingList = ["..

profile on loading

Loading...