: λ€λ₯Έ μμ€ νμΌμ΄λ λͺ¨λμ μ½λμμ μ½λ μΌλΆμ λν μ κ·Όμ μ ν
[μ μ½ μ μ] open < public < internal < fileprivate < private [μ μ½ λ§μ]
μμ μμλ³΄λ€ νμ μμκ° λ λμ μ κ·Ό μμ€μ κ°μ§ μ μμ΅λλ€.
private struct Car {
public var model: String // π¨ μλ¬
}
//privateμ μ μ½μ΄ publicλ³΄λ€ λμ± ν¬κΈ°μ μλ¬κ° λ°μ!
- open : λͺ¨λ μμ€ νμΌμμ ν΄λΉ level μ κ·Ό κ°λ₯
+ λͺ¨λ κ³³μμ μλΈν΄λμ± κ°λ₯
- public : λͺ¨λ μμ€ νμΌμμ ν΄λΉ level μ κ·Ό κ°λ₯
+ κ°μ λͺ¨λ λ΄μμλ§ μλΈν΄λμ± κ°λ₯
- internal : κ°μ λͺ¨λ λ΄μμλ§ μ κ·Ό κ°λ₯
+ μ κ·Ό μ νμλ₯Ό μμ±νμ§ μμΌλ©΄ `internal`λ‘ νλ¨
- fileprivate : κ°μ μμ€νμΌ λ΄μμλ§ μ κ·Ό κ°λ₯
- private : ν΄λμ€ λ΄λΆμμλ§ μ κ·Ό κ°λ₯
fileprivate
-> μλ‘ λ€λ₯Έ ν΄λμ€κ° κ°μ νλμ μμ€ νμΌμ fileprivateλ‘ μ μΈλμ΄ μλ€λ©΄, λ ν΄λμ€λ μλ‘ μ κ·Όν μ μμ
private
-> κ°μ νμΌ μμ μμ΄λ μλ‘ λ€λ₯Έ ν΄λμ€μ΄κ³ ,Β privateλ‘ μ μΈλμ΄ μλ€λ©΄ λ μμλ μλ‘ μ κ·Όν μ μμ
: νλ‘κ·Έλ¨μμ μλ¬κ° λ°μν μν©μ λμνκ³ μ΄μ λμνλ κ³Όμ
Error νλ‘ν μ½μ μ±ννμ¬ μ¬μ©μ μ μ μλ¬λ₯Ό μ μνμ¬ μλ¬ λμ
enum VendingMachineError: Error {
case invalidSelection
case insufficientFunds(coinsNeeded: Int)
case outOfStock
}
throws
λ λ¦¬ν΄ κ°μ λ°ννκΈ° μ μ, μ€λ₯κ° λ°μνλ©΄ μλ¬ κ°μ²΄λ₯Ό λ°νthrows
λ μ€λ₯κ° λ°μν κ°λ₯μ±μ΄ μλ λ©μλ μ λͺ© μμ μ¬μ©throw
λ μ€λ₯κ° λ°μν ꡬκ°μμ μ¬μ©throwλ‘ λμ§ μλ¬λ₯Ό
do-catch
λ¬Έμμ μ²λ¦¬
enum CustomError: Error {
case outOfBounds
case invalidInput(String)
} // Error νλ‘ν μ½μ νμ©νμ¬ μ¬μ©μκ° μ§μ μλ¬ μ μ
func processValue(_ value: Int) throws -> Int {
if value < 0 {
throw CustomError.invalidInput("Value cannot be negative")
} else if value > 100 {
throw CustomError.outOfBounds
}
return value * 2
} // `throw`λ¬Έμ νμ©νμ¬ μ€λ₯κ° λ°μνμμ λμ λμ μ€μ
do {
let result = try processValue(-10)
print("Result is \(result)")
} catch CustomError.outOfBounds {
print("Value is out of bounds!")
} catch CustomError.invalidInput(let errorMessage) {
print("Invalid Input: \(errorMessage)")
} catch {
print("An error occurred: \(error)")
} // `do-catch`λ¬Έμ νμ©νμ¬ μλ¬ μ²λ¦¬
// μΆλ ₯ : Invalid Input: Value cannot be negative
try
do-catch
try?
do-catch
ꡬ문 μμ΄λ μ¬μ©μ΄ κ°λ₯try!
enum MyError: Error {
case invalidInput
}
func someThrowingFunction(value: Int) throws -> String {
guard value >= 0 else {
throw MyError.invalidInput // valueκ° μμμΈ κ²½μ° μλ¬λ₯Ό λμ§
}
return "The value is \(value)"
}
do {
let result = try someThrowingFunction(value: -2)
print(result)
} catch {
print("Error occurred: \(error)") // μμ κ°μ μ²λ¦¬νλ μλ¬
} // do-catchλ¬Έκ³Ό try ν¨κ» μ¬μ©
// try?λ₯Ό μ¬μ©νμ¬ μλ¬ μ²λ¦¬νκΈ°
let result1 = try? someThrowingFunction(value: 5) // μ ν¨ν κ° νΈμΆ
print(result1) // Optional("The value is 5") -> μ΅μ
λ νμ
μΌλ‘ κ²°κ³Ό λ°ν
let result2 = try? someThrowingFunction(value: -2)
print(result2) // nil
let result4 = try! someThrowingFunction(value: -2)
print(result4)
// μλ¬κ° λ°μν μ μλ μν©μμ try! μ¬μ©μΌλ‘ μΈν΄, μλ¬ λ°μ λ° μ± κ°μ μ’
λ£
: Swiftμμμ λ©λͺ¨λ¦¬ κ΄λ¦¬ κΈ°λ² μ€ νλλ‘, κ°μ²΄λ μΈμ€ν΄μ€κ° μ°Έμ‘°λλ νμλ₯Ό μΆμ νμ¬ λ©λͺ¨λ¦¬μμ ν΄μ ν μμ μ κ²°μ
μ°Έμ‘° νμκ°
0
μ΄ λλ©΄ ν΄λΉ κ°μ²΄λ λ©λͺ¨λ¦¬μμ ν΄μ
class Person {
let name: String
init(name: String) {
self.name = name
print("\(name) is being initialized")
}
deinit {
print("\(name) is being deinitialized")
}
}
var reference1: Person?
var reference2: Person?
var reference3: Person?
reference1 = Person(name: "John Appleseed") // RC: 1οΈβ£
// Prints "John Appleseed is being initialized"
reference2 = reference1 // RC: 2οΈβ£
reference3 = reference1 // RC: 3οΈβ£
reference1 = nil // RC: 2οΈβ£ -> μ°Έκ³ νλ κ°μ²΄λ₯Ό μ§μ°λ©΄ RCκ° 1μ© κ°μ
reference2 = nil // RC: 1οΈβ£
reference3 = nil // RC: 0οΈβ£ -> ν΄λΉ κ°μ²΄λ λ©λͺ¨λ¦¬μμ ν΄μ
// Prints "John Appleseed is being deinitialized"
// νλ‘νΌν° μ΅μ λ²μ μ μ¬νκ² μ°Έκ³ μ¬λΆλ₯Ό κ³μ νμΈνμ¬ λ°μ
: λ κ° μ΄μμ μΈμ€ν΄μ€κ° μλ‘κ° μλ‘λ₯Ό κ°ν μ°Έμ‘°μΌ λ λ°μ
κ°ν μ°Έμ‘° μν: λ©λͺ¨λ¦¬κ° ν΄μ λμ§ μκ³ μ μ§λμ΄ λ©λͺ¨λ¦¬ λμκ° λ°μνλ νμ
κ°ν μ°Έμ‘°λ₯Ό μλͺ» μ¬μ©νλ©΄ λ©λͺ¨λ¦¬ λμ(Memory Leak) λ¬Έμ κ° λ°μν μ μμ
weak
λ μ΅μ
λλ‘ μ μΈλλ μ°Έμ‘°
μ°Έμ‘° λμμ΄ λ©λͺ¨λ¦¬μμ ν΄μ λλ©΄ μλμΌλ‘ nilλ‘ μ€μ
λ κ°μ²΄κ° μλ‘λ₯Ό κ°νκ² μ°Έμ‘°νλ κ²½μ°, νμͺ½μ
weak
λ‘ μ μΈνμ¬ μν μ°Έμ‘° λ¬Έμ λ₯Ό ν΄κ²°
class Man {
var name: String
weak var girlfriend: Woman?
//weakλ ν΄λμ€ λ΄λΆκ° μλλΌ μ¬μ©νλ κ°μ²΄ μμ λΆμ¬λ λ¨
init(name: String) {
self.name = name
}
deinit { print("Man Deinit!") }
}
class Woman {
var name: String
var boyfriend: Man?
init(name: String) {
self.name = name
}
deinit { print("Woman Deinit!") }
}
var chelosu: Man? = .init(name: "μ² μ")
var yeonghee: Woman? = .init(name: "μν¬")
chelosu?.girlfriend = yeonghee
yeonghee?.boyfriend = chelosu
chelosu = nil
yeonghee = nil
chelosu?.girlfriend // nil -> `weak`λ₯Ό νμ©ν μλ¬ μ²λ¦¬
unowned
λ μ΅μ
λμ΄ μλ λΉμμ μ°Έμ‘°
λΉμμ μ°Έμ‘°λ νμ κ°μ΄ μλ€κ³ κ°μ νλ©°, μ°Έμ‘°νλ κ°μ²΄κ° ν΄μ λλ©΄ λ°νμ μλ¬κ° λ°μν μ μμ
unowned
μ°Έμ‘°λ μ°Έμ‘° λμμ΄ ν΄μ λ μ μλ κ²½μ°μλ§ μ¬μ©
class Man {
var name: String
unowned var girlfriend: Woman?
init(name: String) {
self.name = name
}
deinit { print("Man Deinit!") }
}
class Woman {
var name: String
var boyfriend: Man?
init(name: String) {
self.name = name
}
deinit { print("Woman Deinit!") }
}
var chelosu: Man? = .init(name: "μ² μ")
var yeonghee: Woman? = .init(name: "μν¬")
chelosu?.girlfriend = yeonghee
yeonghee?.boyfriend = chelosu
yeonghee = nil
chelosu?.girlfriend // μλ¬ λ°μ