μ€λλ 9μκ° λμ΄μμΌ TILμ μμ±νꡬ μλ€μ..!
λ무 μ§μ€μ΄ μλμ μ»€νΌ μνμ μ‘°κΈ νλλ°..
μ¬μ₯μ΄ μΏ΅! μΎ ! μΏ΅! μΎ ! κ³Όλ€ μΉ΄νμΈ μμ·¨λ₯Ό ν κ±° κ°μμ..
λͺΈμ΄ μμ£Ό 무리λ°μ€λ€
κ·Έλ κ² μ€λ μ λ μ ν¨μ€μ λλ€μ..
λ€μ΄μΊμ€ν Error
κ³Όμ λ₯Ό νλ€κ° λ€μ΄ μΊμ€ν μ μ¬μ©ν΄μ κΈ°κΉλκ² λ§λ¬΄λ¦¬νλ €κ³ νλλ°.. λ°λ³΅μ μΌλ‘ μκΈ°λ μ€λ₯!
λ체 λκ° λ¬Έμ μΈμ§ λͺ¨λ₯΄κ² μ΄μ ꡬκΈλ§μ κ±°μ³μ λ€μ΄ μΊμ€ν κ³Ό μ μΊμ€ν λΆν° λ€μ 곡λΆν©λλ€.
μ μΊμ€ν (Upcasting)κ³Ό λ€μ΄μΊμ€ν (Downcasting)
λ λ¨μ΄ λͺ¨λ ν΄λμ€μ μμκ³Ό κ΄λ ¨λ μ©μ΄λ‘ λΆλͺ¨ν΄λμ€μ μμν΄λμ€ μ¬μ΄μ νμ λ³νμ μν΄ μ¬μ©λ©λλ€.
μ μΊμ€ν μ΄λ?
νμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μμ ν΄λμ€ νμ μΌλ‘ λ³ννλ κ²
- [νμ ν΄λμ€μ μΈμ€ν΄μ€] as [μμ ν΄λμ€]
- μμ νκ² μ§νν μ μμ΄ λ°λ‘ κ²μ¬κ° νμνμ§ μλ€.
λ€μ΄μΊμ€ν μ΄λ?
μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό νμ ν΄λμ€ νμ μΌλ‘ λ³ννλ κ²
λ€μ΄μΊμ€ν μ νμ μμ νκ² μ§νλ μ μκΈ° λλ¬Έμ μ€λ₯κ° μκΈΈ μλ μμ΅λλ€.
μ΅μ λ λ°μΈλ©μ νλ κ²κ³Ό κ°μ΄ μΊμ€ν νλ λ°©λ²μ΄ λ κ°μ§ μ‘΄μ¬ν©λλ€.
1. as! (κ°μ λ€μ΄ μΊμ€νΈ)
- [μμ ν΄λμ€μ μΈμ€ν΄μ€] as! [νμ ν΄λμ€]
- κ°μ λ‘ μ λνμ νλ κ±°λ€ λ³΄λ λ°νμ μμ μμ λ€μ΄ μΊμ€νΈλ₯Ό μ±κ³΅νμ§ λͺ»νλ€λ©΄ μλ¬κ° λ©λλ€.
2. as? (μμ ν λ€μ΄ μΊμ€νΈ)
- [μμ ν΄λμ€μ μΈμ€ν΄μ€] as? [νμ ν΄λμ€]
- λ°νμ μμ μμ λ€μ΄ μΊμ€ν μ μ§ννλλ° μ±κ³΅νμ§ λͺ»νλ€λ©΄ nilμ λ°ν> μ΅μ λ νμ μΌλ‘ μ§μ ν΄μ£Όμ΄μΌν¨!
ν΄κ²°
μ κ° μλ¬κ° λ λΆλΆμ λ€μ νμΈν΄λ³΄λ μ λ κ°μ λ€μ΄ μΊμ€νΈλ₯Ό μ§νμ νλλ° λ°νμ μμ μμ μ±κ³΅νμ§ λͺ»ν΄μ μλ¬κ° λ¬μ΅λλ€.
- ν΄λμ€ μ μΈλΆ
class AbstractOperation {
func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return 0.0
}
}
class AddOperation4: AbstractOperation {
override func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return Double(firstNumber + secondNumber)
}
}
class SubtractOperation4: AbstractOperation {
override func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return Double(firstNumber - secondNumber)
}
}
class MultiplyOperation4: AbstractOperation {
override func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return Double(firstNumber * secondNumber)
}
}
class DivideOperation4: AbstractOperation {
override func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return Double(firstNumber) / Double(secondNumber)
}
}
class ModulusOperation4: AbstractOperation {
override func calculate(_ firstNumber: Int ,_ secondNumber: Int) -> Double {
return Double(firstNumber % secondNumber)
}
}
- λ€μ΄μΊμ€ν νμ¬ μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό νμ©νλ €λ ν΄λμ€
class Calculator4 {
var firstNumber: Int = 0
var secondNumber: Int = 0
var op: Character?
init(firstNumber: Int, secondNumber: Int, op: Character? = nil) {
self.firstNumber = firstNumber
self.secondNumber = secondNumber
self.op = op
}
func calculate() -> Double {
var operation = AbstractOperation()
// π¨Error
if op == "+" { operation as! AddOperation4 }
else if op == "-" { operation as! SubtractOperation4 }
else if op == "/" { operation as! DivideOperation4 }
else if op == "*" { operation as! MultiplyOperation4 }
else if op == "%" { operation as! ModulusOperation4 }
else { return 0.0 }
return operation.calculate(firstNumber, secondNumber)
}
}
- Calculator4μ μΈμ€ν΄μ€λ₯Ό λ§λ€μ΄ ν΄λΉ λ©μλλ₯Ό μ΄μ©ν΄ κ° μΆλ ₯
let calculator4 = Calculator4(firstNumber: 30, secondNumber: 6, op: "+")
let addResult = calculator4.calculate()
print("addResult : \(addResult)")
μμ μ½λλ addResultμμ " error: execution was interrupted, reason: signal sigabrt. the process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. " μ΄λ° μλ¬κ° λλλΌκ΅¬μ. κ°μ λ€μ΄ μΊμ€ν μ μ€ν¨νμ λ λ¨λ μλ¬λΌκ³ ν©λλ€.
μ°μ μ μΊμ€ν κ³Ό λ€μ΄μΊμ€ν μ 곡λΆνκ³ λ€μ λ΄€λλ μ κ° λ μ€λ₯λ λ¨μν λ€μ΄ μΊμ€ν λλ¬Έμ λ μ€λ₯κ° μλλλΌκ΅¬μ.. κ°λ μ μΌλ‘ ν μ μλ λ°©λ²μΌλ‘ νλ €κ³ μλνλλ μλμ§..
κ·Έλλ νΉμλ νΉμλ κ°λ₯ν μλ μμΌλ λ΄μΌ νν ¨λκ» μ§λ¬Έμ κΌ ν΄λ΄μΌκ² μ΄μ!
Calculator4μ calculate() λ©μλλ₯Ό μ΄λ κ² κ³ μ³€λλ ν΄κ²°μ΄ λμμ΅λλ€!
func calculate() -> Double {
// π¨Error
// var operation = AbstractOperation()
// if op == "+" { operation as! AddOperation4 }
// else if op == "-" { operation as! SubtractOperation4 }
// else if op == "/" { operation as! DivideOperation4 }
// else if op == "*" { operation as! MultiplyOperation4 }
// else if op == "%" { operation as! ModulusOperation4 }
// else { return 0.0 }
guard let op = op else { return 0.0 }
switch op {
case "+" :
return AddOperation4().calculate(firstNumber, secondNumber)
case "-" :
return SubtractOperation4().calculate(firstNumber, secondNumber)
case "/" :
return DivideOperation4().calculate(firstNumber, secondNumber)
case "*" :
return MultiplyOperation4().calculate(firstNumber, secondNumber)
case "%" :
return ModulusOperation4().calculate(firstNumber, secondNumber)
default :
print("κ³μ°κΈ°μ μ‘΄μ¬νμ§ μλ μ°μ°μμ
λλ€.")
return 0.0
}
}
[ λ΄μΌ ν΄κ²°ν΄μΌ ν λ¬Έμ ]
β νκ°μ§μ λ³μμ λΆλͺ¨ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λμ νκ³ , κ·Έ λ³μλ₯Ό μμ ν΄λμ€λ‘ λ€μ΄μΊμ€ν νμ¬ λ©μλλ₯Ό μ¬μ©νλ €κ³ ν λ, κ²½μ°μ λ°λΌ λ€λ₯Έ λ€μ΄ μΊμ€ν μ νμ¬ μ§ννλ©΄ μλλμ§.. μλλ€λ©΄ μ μλλμ§..!!!!
μ°Έκ³ ν λΈλ‘κ·Έ π https://babbab2.tistory.com/127