[Swift๐Ÿฆฉ] # 1 ๊ธฐ๋ณธ, #2 ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž

๋˜์ƒยท2022๋…„ 3์›” 18์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
14/42
post-thumbnail

์Šคํ„ฐ๋””

iOS ์Šคํ„ฐ๋””๋ฅผ ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ํ”„๋กœ์ ํŠธ ๋ฒŒ๋ ค๋†“์€ ๊ฒƒ๋„ ๋ง‰๋ง‰ํ•˜๊ณ  ๋ฌธ๋ฒ• ๊ฐ€์ด๋“œ๋„ ๋ช‡ ๋ฒˆ ์ฝ์—ˆ์ง€๋งŒ ์™„๋ฒฝํ•˜๊ฒŒ ์•„๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ๋ฌธ๋ฒ• ์Šคํ„ฐ๋””๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

  • ๊ธฐ๊ฐ„ : 0321~0410 (4์ฃผ)
  • ๋‚ด์šฉ
    • ๋ฌธ๋ฒ• ๊ฐ€์ด๋“œ ํ‰์ผ ํ•˜๋ฃจ์— ํ•œ ์ฃผ์ œ์”ฉ ์ฝ๊ณ , ์š”์•ฝ ์ •๋ฆฌ
      https://bbiguduk.gitbook.io/swift/
    • ๋ชจ๋ฅด๋Š” ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด ๋” ์ฐพ์•„๋ณด๊ธฐ
    • iOS ์ธํ„ฐ๋ทฐ ๋งํฌ์— ํ•ด๋‹น ์ฃผ์ œ์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ด ์žˆ์œผ๋ฉด, ํ•ด๋‹น ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๊นŒ์ง€ ์ƒ๊ฐํ•ด์˜ค๊ธฐ
      https://github.com/JeaSungLEE/iOSInterviewquestions
    • ์ผ์ฃผ์ผ์— ํ•œ ๋ฒˆ ์˜จ๋ผ์ธ์œผ๋กœ ์„œ๋กœ ์š”์•ฝ ์ •๋ฆฌ ํ•œ ๊ฒƒ์„ ๊ณต์œ /๋ฐœํ‘œํ•˜๊ณ , ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„ ํ† ๋ก 



๋ณ€์ˆ˜ ์„ ์–ธ, ๋ฐ˜๋ณต๋ฌธ, ํ•จ์ˆ˜ ๋“ฑ์€ ๋ฐœํ‘œํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ž ์ฝ๊ธฐ๋กœ ํ–ˆ๋‹ค.

1. ๊ธฐ๋ณธ

1. ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ (Constant And Variable)

let, var - ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฐ’์—๋Š” let ์„ ์‚ฌ์šฉํ•˜์ž.

let title: String = "ํƒ€์ž… ๋ช…์‹œ."
// let typeSafety: Int = "์ด๊ฑด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค"
let content = 
"""
์ž๋™์œผ๋กœ ํƒ€์ž…์„ ์œ ์ถ”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 
ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ฉด 
์“ธ ํ•„์š” ์—†๋‹ค.
"""

let naming๐Ÿ“›: String = "์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„๋งŒ ์•„๋‹ˆ๋ฉด ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ๊ฐ€๋Šฅ"

print(naming๐Ÿ“›, seperator : , terminator: " ")
/* python 
print(naming, sep=",", end=" ") ์™€ ๊ฐ™์Œ. */

print("A"); print("B")

๐Ÿ“• Swift ์˜ ํƒ€์ž…์€ ๊ตฌ์กฐ์ฒด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

2. ์ •์ˆ˜, ์‹ค์ˆ˜

ํŠน๋ณ„ํ•œ ์ผ€์ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ •์ˆ˜๋Š” Int (ํ”Œ๋žซํผ์— ๋”ฐ๋ผ 32, 64 ๋น„ํŠธ ๊ฒฐ์ •๋จ)3, ์‹ค์ˆ˜๋Š” Double ์„ ์‚ฌ์šฉํ•˜์ž.

๐Ÿ“• ํŠน๋ณ„ํ•œ ์ผ€์ด์Šค : Int.max ~ UInt.max ์‚ฌ์ด์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•ด์•ผํ•  ๋•Œ!

Int32, UInt8  // signed / unsigned + bit
Int32.min, Int32.max  // ์ •์ˆ˜ ๋ฒ”์œ„ ์ตœ๋Œ€ ์ตœ์†Œ
Int, UInt // ํ”Œ๋žซํผ์— ๋”ฐ๋ผ 32bit, 64bit ๊ฒฐ์ •๋จ.

// 0b 0o 0x
let decimal = 8
let binary = 0b1000
let octal = 0o10
let hexadecimal = 0x8

Type Safety and Type Inference โญ๏ธ

Double // 64 bit - 15์ž๋ฆฌ ์†Œ์ˆ˜์  ์ •ํ™•๋„ ๐Ÿ‘
Float // 32 bit - 6์ž๋ฆฌ

// ์‹ค์ˆ˜ ํ‘œํ˜„ ๋ฐฉ์‹
let decimalDouble = 12.1875
let exonentDouble = 1.21875e1
let hexadecimalDouble = 0xC.3p0

// ์ถ”๊ฐ€ ํฌ๋งท ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1

Nuemeric Type Conversion โญ๏ธ

// ์„œ๋กœ ๋‹ค๋ฅธ ์ •์ˆ˜/์‹ค์ˆ˜ ๋ผ๋ฆฌ์˜ ์—ฐ์‚ฐ์—๋Š” ๋ช…์‹œ์ ์ธ ํƒ€์ž… ๋ณ€ํ™˜ ํ•„์š”.
let one = 1
let two: UInt16 = 2
let three = UInt16(1) + 2

let double = Double(one) + 2.376

๐Ÿ“• random(in: ) ์œผ๋กœ ์ž„์˜์˜ ์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

Int.random(in: -1...100)
UInt.random(in: 1...30)
Double.random(in: 2.3..<11.2)
Float.random(in: -1.3..<1.3)

3. Type Aliases

// ์กด์žฌํ•˜๋Š” ํƒ€์ž…์„ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ •์˜
typealias Sample = Int
typealias VideoCell = UICollectionViewVideoCell

4. Booleans

let ์ง„์งœ: Bool = true // != 1
let ๊ฐ€์งœ: Bool = false // != 0

5. Tuples โญ๏ธ

// ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๋ฌถ์–ด์„œ ์ œ๊ณต.
let http404Error = (404, "Not Found")
let xyz = (10, 3, 1)

// ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๊ฐ€๋Šฅ.
let (statusCode, statusMessage) = http404Error
let (statusCode, _) = http404Error

// ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
let http200Status = (statusCode: 200, description: "OK")
print(http200Status.description)

6. Optionals โญ๏ธโญ๏ธ

๊ฐ’์ด ์žˆ์œผ๋ฉด -> ํ’€์–ด์„œ ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
๊ฐ’์ด ์—†์œผ๋ฉด -> nil

var surveyAnswer: String? // = nil ํ•˜์ง€ ์•Š์•„๋„ nil๋กœ ์ดˆ๊ธฐํ™”.

๐Ÿ“• ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์ž˜๋ชป๋œ ๊ฐ’์ผ ๊ฒฝ์šฐ nil ์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ์Œ์„ ํ‘œ์‹œ ํ•œ๋‹ค๋“ ์ง€
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตณ์ด ๋„˜๊ธฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๋œป

enum ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ. -> switch ๋ฅผ ํ†ตํ•œ ๊ฒ€์‚ฌ ๊ฐ€๋Šฅ. -> where ์ ˆ๊ณผ ํ•ฉ์น˜๋ฉด ์„ธ๋ถ€์‚ฌํ•ญ๊นŒ์ง€๋„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ -> ํ•˜์ง€๋งŒ ๊ท€์ฐฎ์•„์„œ Optinal Binding ๋“ฑ์˜ ๋ฐฉ๋ฒ• ์‚ฌ์šฉ.

public enum Optinal<Wrapped>: ExpressibleByNilLiteral {
    case none
    case some(Wrapped)
    public init(_ some: Wrapped) {
        // ...
    }
}

unwrapping

๊ฐ•์ œ ์–ธ๋ž˜ํ•‘ !

if surveyAnswer != nil {
    print(surveyAnswer!) 
}

Optional Binding

if let ๊ตฌ๋ฌธ.

if let actualNumber = Int(possibleNumber) {
    print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
    print("The string \"\(possibleNumber)\" could not be converted to an integer")
}

์•”์‹œ์ ์œผ๋กœ unwrapping ๋œ optinal

๐Ÿ“• ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ.

  • nil ์„ ๊ฐ€์งˆ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ,
  • Optinal Binding ์œผ๋กœ ๋งค๋ฒˆ ๊ฐ’์„ ์ถ”์ถœํ•˜๊ธฐ๋Š” ๊ท€์ฐฎ์œผ๋ฉด์„œ..
  • ๋กœ์ง์ƒ nil ๋•Œ๋ฌธ์— ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ทธ์น˜๋งŒ ์—ญ์‹œ 100% ์•ˆ์ „ํ•˜์ง„ ์•Š์œผ๋ฏ€๋กœ Optional Binding ์„ ํ•˜์ž!!
// ์˜ต์…”๋„ ์ •์˜ ์งํ›„์— ๊ฐ’์„ ํ•ญ์ƒ ๊ฐ€์ง€๋ฉด (== nil์ด ๋  ์ผ์ด ์—†์œผ๋ฉด)
let possibleString: String? = "An optional string."
let forcedString: String = possibleString!

// String! ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ์Œ.
var assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString

assumedString = nil // ๊ฐ€๋Šฅํ•˜๊ธด ํ•œ๋ฐ,
print(assumedString) // nil ์ด ํ• ๋‹น๋˜์–ด ์žˆ์„ ๋•Œ ์ ‘๊ทผํ•˜๋ฉด ์˜ค๋ฅ˜

@IBAction private var label: UILabel!

7. Error Handling โญ๏ธ

1. throw - try - catch

func canThrowAnError() throws {
    // this function may or may not throw an error
}

do {
    try canThrowAnError()
    // no error was thrown
} catch ํŠน์ •์—๋Ÿฌ { 
    // an error was thrown
} catch ์ธ์ž๋ฅผ๊ฐ€์ง€๋Š”ํŠน์ •์—๋Ÿฌ(let ์ธ์ž) {
    
} catch {
    // ์œ„์—์„œ ์•ˆ์žกํžŒ ๋ชจ๋“  ์—๋Ÿฌ ์žก์Œ.
}

8. Assertion and Preconditions

๋Ÿฐํƒ€์ž„์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด.

์ „์ œ ์กฐ๊ฑด์ด true ์ด๋ฉด ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์ง„ํ–‰๋˜๊ณ ,
false ๋ฉด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ฝ”๋“œ ์‹คํ–‰๊ณผ ์•ฑ์ด ์ข…๋ฃŒ๋œ๋‹ค.
์œ ํšจํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ฅผ ๋ง‰์•„์„œ ํ”ผํ•ด๋ฅผ ์ œํ•œ.

์ฐจ์ด๋Š”?
Assertion - ๊ฐœ๋ฐœ ์ค‘์— ์‹ค์ˆ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋””๋ฒ„๊น…ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ. (debug build ์—์„œ๋งŒ ์‚ฌ์šฉ)

let age = -3
assert(age >= 0, "A person's age can't be less than zero.")
// ๋‹ค์Œ ์ฝ”๋“œ


// ์กฐ๊ฑด์ด ์ด๋ฏธ ์ฒดํฌ๋˜์—ˆ์„ ๋•Œ๋Š” assertionFailure ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Œ.
if age > 10 {
    print("You can ride the roller-coaster or the ferris wheel.")
} else if age >= 0 {
    print("You can ride the ferris wheel.")
} else {
    assertionFailure("A person's age can't be less than zero.")
}

Precondition - debug / production build ์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉ.

precondition(index > 0, "Index must be greater than zero.")

preconditionFailure(_:file:line:) 

assertion / assertionFailure
precondition / preconditionFailure ๋Š” ๋˜ ์ฒดํฌ ํ•˜๋Š”์ง€ ์•ˆํ•˜๋Š”์ง€์— ๋”ฐ๋ฅธ ์กฐ๊ฑด์ด ์ข€ ๋‹ค๋ฅด์ง€๋งŒ
-> ์ปดํŒŒ์ผ Optimization level ์— ๋”ฐ๋ผ ์ฒดํฌ ํ•˜๋Š”์ง€ ์•ˆํ•˜๋Š”์ง€๊ฐ€ ์ข€ ๋‹ฌ๋ผ์ง„๋‹ค.
https://eunjin3786.tistory.com/453



2. ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž

๋‹จํ•ญ / ์ดํ•ญ / ์‚ผํ•ญ ์—ฐ์‚ฐ์ž
์—ฐ์‚ฐ์ž / ํ”ผ์—ฐ์‚ฐ์ž

1. ๋Œ€์ž…, ์‚ฐ์ˆ , ๋น„๊ต

1. ๋Œ€์ž…

let x = 20

if x = 20 // ๋ถˆ๊ฐ€๋Šฅ. js ์˜ ํ‘œํ˜„์‹์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ.
+= -= *= /= %=

2. ์‚ฐ์ˆ 

+ - * / %

// ๋‹จํ•ญ
-3 // -3
+(-3) // -3

3. ๋น„๊ต

== != > < >= <=
**๐Ÿ“• === !== (์ฐธ์กฐ๊ฐ€ ๊ฐ™๋‹ค ๋‹ค๋ฅด๋‹ค) ~= (ํŒจํ„ด ๋งค์น˜)**

// ํŠœํ”Œ์˜ ๊ฒฝ์šฐ ์™ผ์ชฝ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ๊ฐ™์œผ๋ฉด ๊ทธ ๋‹ค์Œ ๊ฐ’ ... 
(1, 2, 3) < (1, 2, 4) // true
(1, 2, 3) == (1, 2, 3) // true

2. ์‚ผํ•ญ ์กฐ๊ฑด โญ๏ธ

// question ์ด true ์ด๋ฉด answer1 ์•„๋‹ˆ๋ฉด answer2
question ? answer1 : answer2

3. nil ๊ฒฐํ•ฉ โญ๏ธ

a ?? b

a != nil ? a! : b // ์ด๊ฒƒ๊ณผ ๊ฐ™์Œ.

4. ๋ฒ”์œ„ โญ๏ธ

1...5  // 1~5
1..<5 // 1~4
2...
...2
..<2

for i in 1..<5 {
    // ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
}

// ์–ด๋””์„œ ์‹œ์ž‘ํ•  ์ง€ ํ™•์‹คํ•œ ๊ฒฝ์šฐ๋งŒ ์‹œ์ž‘ ๊ฐ’ ์ƒ๋žต ๊ฐ€๋Šฅ.
for name in names[...5] { 
    print(name)
}

5. ๋…ผ๋ฆฌ

! && ||



profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€