Swift

    [Swift] inout? | ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์ง€์†ํ•˜๊ธฐ

    What is inout? Swift์—์„œ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ƒ์ˆ˜์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ ํ•จ์ˆ˜์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธธ ์›ํ•˜๊ณ  + ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ข…๋ฃŒ๋œ ํ›„์—๋„ ์ง€์†๋˜๊ธธ ์›ํ•  ๋•Œ inout ์‚ฌ์šฉ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๊ฐ’์„ ๋„˜๊ฒจ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ func increment(_ number: Int){ number += 1 print(number) } increment(3) // Error: 'number' is a 'let' constant var number = 3 func increment(_ number: inout Int){ number += 1 print(number) } increment(&number) // 4 How to use inout?..

    [Swift] Protocols | Delegate๋ฅผ ์‚ด์ง ๊ณ๋“ค์ธ

    ํ”„๋กœํ† ์ฝœ์ด๋ž€? ํ”„๋กœํ† ์ฝœ: ์ตœ์†Œํ•œ์œผ๋กœ ๊ฐ€์ ธ์•ผํ•  ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น Object๊ฐ€ task๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์„ ์ •์˜ํ•ด์ค€๋‹ค. Java์˜ interface์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. protocol์€ ์„ ์–ธ์ด๊ณ , type์—์„œ ๊ตฌํ˜„ํ•ด์ค˜์•ผํ•œ๋‹ค. ex1) Swift์—์„œ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœํ† ์ฝœ ์˜ˆ์‹œ ์ค‘ ํ•˜๋‚˜์ธ CustomStringConvertible์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ํ‹€๋Œ€๋กœ class๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. var greeting = "Hello, playground" class person: CustomStringConvertible { let name: String let age: Int let isFemale: Bool init(name: String, age: Int..

    [Swift] Guard๋ฌธ | guard let

    Guard๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•  ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜์—ฌ ๊ทธ ๋‹ค์Œ์— ์˜ค๋Š” ์ฝ”๋“œ๋“ค์„ ์‹คํ–‰ํ• ์ง€ ๋ง์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. Guard๋ฅผ ์ด์šฉํ•˜๋Š” ์ด์œ ๋ฅผ ์ •๋ฆฌํ•˜์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 1. control flow์™€ indentation์„ ๋‹จ์ˆœํ•˜๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด → ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง€๊ณ  ๊ทธ๋กœ ์ธํ•ด ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 2. ๋ถ€์ ์ ˆํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์—†์• ๋ฒ„๋ฆฌ๊ธฐ ์œ„ํ•ด if๋ฌธ vs guard๋ฌธ if๋ฌธ ์‚ฌ์šฉ ver. func singHappyBirthday(){ if(birthdayIsToday){ if(invitedGuests > 0){ if (cakeCandleLit){ print("Happy Birthday to you!") }else{ print("์ผ€์ต ์ด›๋ถˆ ๋ถˆ ์•ˆ์ผœ์ง") } }else{ print("์ดˆ๋Œ€ ์•„๋ฌด๋„ ์•ˆํ•จ") } }else{ p..

    [Swift] Optionals

    ๋Œ๋‹ค๋ฆฌ๋„ ๋‘๋“ค๊ฒจ๋ณด๊ณ  ๊ฑด๋„ˆ์ž. Optional: ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ์—†์„ ์ˆ˜๋„ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ Optional์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” 2๊ฐ€์ง€ Case๊ฐ€ ์žˆ๋‹ค. (1) ์–ด๋–ค ๊ฐ’์ด ์žˆ์–ด์„œ ๊ทธ ๊ฐ’์„ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ (2) ์•„์˜ˆ ๊ฐ’์ด ์—†์–ด์„œ ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ - nil: ์•„์ง ๊ฐ’์ด ์—†๋‹ค. โ€ป ํ•„์š”์— ๋”ฐ๋ผ์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์˜ต์…”๋„๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ต์…”๋„์„ ์ •์˜ํ•˜๋Š” ๋ฒ• Optional์„ ์ •์˜ํ•  ๋•Œ๋Š” ์–ด๋–ค Type ๋’ค์— ?๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค. ์ด ๋•Œ, Optional์„ ์“ธ ๋•Œ๋Š” ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด์˜ฌ์ง€ ๋ชจ๋ฅด๊ธฐ์— Type - Inferring์ด ์•ˆ๋˜๋ฏ€๋กœ Type์„ ๋ฐ˜๋“œ์‹œ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค. Optional์—์„œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• | Optional Binding let userInputAge..

    [Swift] ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ง„ ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

    Array: repeating repeating์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ํŠน์ • ๊ฐ’์„ array ์•ˆ์— ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. - ํŠน์ • ์œ„์น˜์— ๊ฐ’์ด ์—†์œผ๋ฉด ์•ฑ์ด crash ๋‚  ๊ฒฝ์šฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ํŠน์ • ์ธ๋ฑ์Šค๊นŒ์ง€๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. var myArray = [Int](repeating: 0, count: 100)

    [Swift] Class์™€ Inheritance(์ƒ์†)

    Inheritance A parent class = 'Superclass' A child class = 'Subclass' ์Šˆํผ ํด๋ž˜์Šค ์—†์ด ์ •์˜ํ•œ ํด๋ž˜์Šค = 'Base class' class Bicycle: Vehicle { var hasBasket = false } let bicycle = Bicycle() bicycle.hasBasket = true Override property์™€ method๋Š” ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๋ž˜ ์žˆ๋˜ ์†์„ฑ์— ์˜ฌ๋ผํƒ€์„œ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ. ํ•จ์ˆ˜ ์ด๋ฆ„ ์˜†์— override ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ์‚ฌ์šฉ ๊ฐ€๋Šฅ. class Train: Vehicle{ override func makeNoice(){ print("Choo Choo!") } } Reference class Person{ le..

    [Swift] Structure

    โ€ป convention: ๊ตฌ์กฐ์ฒด์˜ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž, ๊ทธ ์•ˆ์— property๋‚˜ method๋“ค์€ ์†Œ๋ฌธ์ž, ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์†Œ๋ฌธ์ž instance ์ธ์Šคํ„ด์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์•ผํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋Š” structure์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์™€ ๊ธฐ๋Šฅ๋“ค์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. struct User { static var currentUser: User = User(userName: "Default", email: "Default", age: 0) var userName: String var email: String var age: Int static func logIn(user: User){ currentUser = user } } let me = User(userName: "Harry", email: "djk..

    [Swift] ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ์„œ์˜ ํ•จ์ˆ˜

    Swift๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ํฌํ•จํ•˜๋Š” ๋‹ค์ค‘ ํŒจ๋Ÿฌ๋‹ค์ž„ ์–ธ์–ด ์Šค์œ„ํ”„ํŠธ์˜ ํ•จ์ˆ˜๋Š” ์ผ๊ธ‰๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ๋“ฑ์— ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํƒ€์ž…์ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋Š” ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค. (๋งค๊ฐœ๋ณ€์ˆ˜ 1ํƒ€์ž…, ๋งค๊ฐœ๋ณ€์ˆ˜2ํƒ€์ž… ...) -> ๋ฐ˜ํ™˜ํƒ€์ž… func greeting(to friend: String, from me: String){ print("Hello \(friend)! I'm \(me)") } var someFunction: (String, String) -> Void = greeting(to:from:) someFunction("a","finit") // someFunction ์ž์ฒด๊ฐ€ ํ•จ์ˆ˜์ด๊ธฐ์— ํ˜ธ์ถœ ๊ฐ€๋Šฅ // Hello a! I'm finit. someFunction = ..