μ€λμ ν΄λμ€μ ꡬ쑰체 κ·Έλ¦¬κ³ μ΄κ±°ν !
π class π
class Circle { // classλͺ
μ λλ¬Έμλ‘ μμ
var radius = 3.123 // class μμ±
let pi = 3.1415926535
func area() -> Double { // λ©μλ
return (radius * radius * pi)
}
}
let circle = Circle() // μΈμ€ν΄μ€(κ°μ²΄, μ€λΈμ νΈ) μμ±
let circle = Circle.init() // μμ κ°μ
circle.area()
νλ‘νΌν°κ° μ΄κΈ°νλΌμμ§ μμΌλ©΄ μλ¬
- class μμ
class Apple { // super class: 곡ν΅
var appleColor = "red"
}
class AppleAndBanana : Apple { // sub class: μΆκ°
var bananaColor = "yellow"
}
let fruit = AppleAndBanana()
fruit.appleColor
fruit.bananaColor
κ³΅ν΅ κΈ°λ₯λ€μ μνΌ ν΄λμ€λ‘ λ¬Άκ³ μμμ λ°μ λ€μν μλΈ ν΄λμ€ λ§λ€μ΄ μ¬μ©
- override
class Apple { // super class: 곡ν΅
var appleColor = "red"
func taste() {
print("yum !")
}
}
class AppleAndBanana : Apple { // sub class: μΆκ°
var bananaColor = "yellow"
// override ν΅ν΄ μ¬μ μ
override func taste() {
print("super ")
super.taste() // super classλ₯Ό μλ―Έ
}
}
let fruit = AppleAndBanana()
fruit.appleColor
fruit.bananaColor
fruit.taste()
- μμ±μ init
class Circle {
var radius: Double
let pi = 3.1415926535
// init μ¬λ¬ κ° μμ± κ°λ₯
init() {
radius = 10.0
}
init(radius: Double) {
self.radius = radius
}
func area() -> Double {
return (radius * radius * pi)
}
}
let circle = Circle.init(radius: 20.0)
circle.area()
// μμ±μ ν¨μλ‘ μμ±
var num = Int.init(10)
var message = String("Hello")
var array = [Int].init(arrayLiteral: 1, 2, 3, 4)
.init μλ΅ κ°λ₯
- ν΄λμ€μ ꡬ쑰체
- 곡ν΅μ
- μμ± μ μ
- λ©μλ μ μ
- μμ±μλ₯Ό ν΅ν μ΄κΈ° μ€μ
- Extension, protocol μ¬μ© κ°λ₯
- ν΄λμ€λ§μ νΉμ§
- μμ
- νμ μΊμ€ν
- μλ©Έμλ₯Ό ν΅ν 리μμ€ μ 리 (deinit)
- μ°Έμ‘° νμ
- ꡬ쑰체 μ¬μ©νλ κ²½μ°
- λͺ κ°μ§ κ°λ¨ν λ°μ΄ν° κ°μ μΊ‘μν
- ν λΉ λ° μ λ¬ μ 볡μ¬κ° ν©λ¦¬μ
- λͺ¨λ μμ±λ κ° νμ -> 볡μ¬κ° λ§μ λ
- μμμ΄ νμ μμ λ
- λλΆλΆ class μ¬μ©
- 곡ν΅μ
π μ΄κ±°ν π
enum Fruit {
case apple
case banana
case watermelon
}
var fruit: Fruit = .watermelon
func printFruit(fruit: Fruit) {
switch fruit {
case .apple:
print("apple !")
case .banana:
print("banana !")
case .watermelon:
print("watermelon π")
}
// μ΄κ±°νμ μ’
λ₯κ° νμ -> default νμ X
}
printFruit(fruit: .watermelon)
defaultκ° νμ μλ€ → μ’ λ₯κ° κ³ μ λμ΄ μκΈ° λλ¬Έ
- rawValue
enum Fruit: String{
case apple = "π"
case banana = "π"
case watermelon = "π"
}
var fruit: Fruit = .watermelon
print(fruit.rawValue)
κ°λ λ£μ μ μμ
μ΄μ λ¨μ κ°μλ λ€μΌλ¬ κ°μΌμ§ !
'c o d i n g . . π > ios' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μΈνλ°] μμ΄ν° μ± κ°λ°(4) (0) | 2022.06.22 |
---|---|
[μΈνλ°] μμ΄ν° μ± κ°λ°(2) (0) | 2022.06.20 |
[μΈνλ°] μμ΄ν° μ± κ°λ°(1) (0) | 2022.06.17 |