인스턴스 만들고 메서드와 프로퍼티 접근
class Man{
var age : Int = 32
var weight : Double = 82.3
func display(){
print("나이=(age), 몸무게=(weight)") }
}
var yoo : Man = Man()
yoo.display()
// 나이=32, 몸무게=82.3
클래스 메서드
class Man{
var age : Int = 32
var weight : Double = 82.3
func display(){
print("나이=(age), 몸무게=(weight)") }
class func cM(){
print("cM은 클래스 메서드입니다.")
}
static func scM() {
print("scM은 클래스 메서드(static)")
}

}
var yoo : Man = Man() // initalize
yoo.display()
Man.cM()
Man.scM()
// 나이=32, 몸무게=82.3
// cM은 클래스 메서드입니다.
// scM은 클래스 메서드(static)
인스턴스 초기화하기 : init()
class Man{
var age : Int = 32
var weight : Double = 82.3
func display(){
print("나이=(age), 몸무게=(weight)") }
init(yourAge: Int, yourWeight : Double){
age = yourAge
weight = yourWeight
} // designated initiallizer

}
var yoo : Man = Man(yourAge: 14, yourWeight: 33.8)
yoo.display()
// 나이=14, 몸무게=33.8
과제: 초기값 1과 3.5는 생략할 수 있나?
initializer로 클래스 값이 초기화가 되기 때문에 없어도 상관이 없다.

self
class Man{
var age : Int = 32
var weight : Double = 82.3
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}

}
var yoo : Man = Man(age: 14, weight: 33.8)
yoo.display()
// 나이=14, 몸무게=33.8
현재 클래스 내 메서드나 프로퍼티를 가리킬 때 메서드나 프로퍼티 앞에 self. 을 붙인다
매개변수와 메서드나 프로퍼티 이름이 같을 때에 구분 하기 위해 반드시 사용해야함
Stored property와 computer property
class Man{
var age : Int = 32
var weight : Double = 82.3
var manAge : Int{ //메서드 같지만 computed property임
// get{ setter가 없을 경우에 생략할 수 있음
return age-1
// }
}
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}

}
var yoo : Man = Man(age: 14, weight: 33.8)
yoo.display()
print(yoo.manAge)
// 나이=14, 몸무게=33.8
// 13

computer property의 setter
class Man{
var age : Int = 32
var weight : Double = 82.3
var manAge : Int{ //메서드 같지만 computed property임
get{
return age-1
}
set(USAAge){
age = USAAge + 1
}
}
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}

}
var yoo : Man = Man(age: 14, weight: 33.8)
yoo.display()
print(yoo.manAge)
print(yoo.age)
yoo.manAge = 5 //setter호출
print(yoo.age)
// 나이=14, 몸무게=33.8
// 13
// 14
// 6
computer property의 getter와 setter
class Man{
var age : Int = 32
var weight : Double = 82.3 // stored property
var manAge : Int{
get{ return age-1 }
set{ age = newValue + 1 }
} // computed property
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}

}
var yoo : Man = Man(age: 14, weight: 33.8)
yoo.display()
print(yoo.manAge)
print(yoo.age)
yoo.manAge = 5
print(yoo.age)
// 나이=14, 몸무게=33.8
// 13
// 14
// 6

Method overloading : 생성자 중첩
class Man{
var age : Int = 32
var weight : Double = 82.3

func display(){
    print("나이=\(age), 몸무게=\(weight)") }
init(age: Int, weight : Double) {
    self.age = age
    self.weight = weight
}
init(age: Int) {
    self.age = age
}

}
var yoo : Man = Man(age: 14, weight: 33.8)
var yoo1 : Man = Man(age: 18)
yoo.display()
yoo1.display()
// 나이=14, 몸무게=33.8
// 나이=18, 몸무게=82.3
Failable Initializers
let myImage: UIImage = UIImage(named: "apple.png")!
apple.png 파일이 없거나 오타로 ppale.png로 입력하여도 오류가 나는 것이 아닌 nil값으로 출력
! 나 ? 로 설정 가능하며 옵셔널 형으로 변환한다.
Failable Initializer가 있는 클래스의 인스턴스 생성
class Man{
var age : Int
var weight : Double

func display(){
    print("나이=\(age), 몸무게=\(weight)") 
}

init?(age: Int, weight : Double) {
    if age <= 0 {
        return nil
    }
    else {
        self.age = age
    }
    self.weight = weight
}// failable initialize

}
var yoo : Man? = Man(age: 14, weight: 33.8) // 1-1 옵셔널 형으로 선언
if let yoo1 = yoo { // 1-2 옵셔널 바인딩
yoo1.display()
}
if let yoo2 = Man(age:3, weight: 5.8){ // 2 인스턴스 생성과 동시에 옵셔널 바인딩
yoo2.display()
}
var yoo3 : Man = Man(age:5, weight:6.9)! // 3 인스턴스 생성하면서 바로 강제 언래핑
yoo3.display()
var yoo4 : Man? = Man(age:4, weight: 10.5) // 4 옵셔널 인스턴스를 사용 시 강제 언래핑
yoo4!.display
// 나이=14, 몸무게=33.8
// 나이=3, 몸무게=5.8
// 나이=5, 몸무게=6.9
Failable Initializer가 nil반환
class Man{
var age : Int
var weight : Double

func display(){
    print("나이=\(age), 몸무게=\(weight)") 
}

init?(age: Int, weight : Double) {
    if age <= 0 {
        return nil
    }
    else {
        self.age = age
    }
    self.weight = weight
}// failable initialize

}
var yoo : Man? = Man(age:-1, weight:3.5) //옵셔널 형으로 선언
if let yoo1 = yoo { //옵셔널 바인딩 kim1.display()
} // nil
//인스턴스 생성과 동시에 옵셔널 바인딩
if let yoo2 = Man(age:0, weight:5.5) {
yoo2.display()
}
// var yoo3 : Man = Man(age:0, weight:7.5)!
// yoo3.display()
//인스턴스 생성하면서 바로 강제 언래핑 //crash!!
//강제 언래핑하는 방법은 위험함
과제 : 나이와 몸무게가 음수이면 nil을 반환하는 failable initialize 구현
class Man{
var age : Int
var weight : Double

func display(){
    print("나이=\(age), 몸무게=\(weight)") 
}

init?(age: Int, weight : Double) {
    if age <= 0 {
        return nil
    }
    else {
        self.age = age
    }
    self.weight = weight
}// failable initialize

}
var yoo : Man? = Man(age: -20, weight: -59.3)
if let yoo1 = yoo{
yoo1.display()
}
if let yoo2 = Man(age: 3, weight: -32.2){
yoo2.display()
}
// 나이=3, 몸무게=-32.2

iOS UIKit class hierarchy에서 init() 찾아 간단 사용 예
class UIColor{
let red, green, blue, alpha : CGFloat
init?(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat){
if(red <= 0){
return nil
}
else{
self.red = red
}
self.green = green
self.blue = blue
self.alpha = alpha
} // failable initializer
init(blue: CGFloat){
red = blue
green = blue
blue = blue
alpha = blue
}// overloading : 같은 이름의 init함수가 여러 개 있는 것이다.
let skyBlue = UIColor(blue : 0.5) //blue를 0.5로 초기화
if let greenRGB = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0) // 인스턴스 생성과 동시에 옵셔널 바인딩
상속 : 부모가 가진 것을 물려받아요.
class Man{
var age : Int = 1
var weight : Double = 3.5
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}
}
class Student : Man { //비어있지만 Man의 모든 것을 가지고 있음
}
var yoo : Man = Man(age:4, weight:12.3)
yoo.display()
var kim : Student = Student(age:34,weight:76.2)
kim.display()
print(kim.age)
// 나이=4, 몸무게=12.3
// 나이=34, 몸무게=76.2
// 34

Super : 부모 메서드 호출 시 사용
class Man{
var age : Int
var weight : Double
func display(){
print("나이=(age), 몸무게=(weight)")
}
init(age: Int, weight : Double){ self.age = age
self.weight = weight
}

}
class Student : Man {
var name : String
func displayS() {
print("이름=(name), 나이=(age), 몸무게=(weight)")
}
init(age: Int, weight : Double, name : String){
self.name = name
super.init(age:age, weight:weight) //과제: 이 줄을 안쓰면?
}
}
var yoo : Student = Student(age:32,weight:98.4,name:"JaeWoo")
yoo.displayS()
yoo.display()
// 이름=JaeWoo, 나이=32, 몸무게=98.4
// 나이=32, 몸무게=98.4

Override : 부모와 자식에 같은 메서드가 있으면 자식우선
class Man{
var age : Int = 5
var weight : Double = 2.4
func display(){
print("나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double){
self.age = age
self.weight = weight
}
}
class Student : Man {
var name : String = "YJW"
override func display() {
print("이름=(name), 나이=(age), 몸무게=(weight)") }
init(age: Int, weight : Double, name : String){
super.init(age:age, weight:weight)
self.name = name
}
}
var yoo : Student = Student(age:29,weight:99.9,name:"JAEWOO")
yoo.display()
// 이름=JAEWOO, 나이=29, 몸무게=99.9

profile
끝없이 탐구하는 iOS 개발자 유재우입니다!

0개의 댓글