Swift Basic (6)

Harold's velog·2024년 1월 31일

iOSbasic

목록 보기
13/15

사용자 정의 타입.

  1. 구조체
  • 스위프트 대부분의 타입은 구조체로 이루어져 있다.

  • 구조체는 값 타입이다.

  • 타입이름은 대문자 카멜케이스를 사용하여 정의한다.

  • 구성

struct <#이름#> {
    <#구현부#>
}
  • 프로퍼티 및 메서드
struct Sample {
    // 가변 프로퍼티 (var)
    var mutableProperty: Int = 100
    
    // 불변 프로퍼티 (let)
    let immutableProperty: Int = 100
    ==================================
    // 위의 두 프로퍼티는 인스턴스 프로퍼티이다(Instance Property)
   
   	// 타입 프로퍼티 - static을 사용
    static var typeProperty: Int = 100
    
    // 인스턴스 메서드
    func instanceMethod() {
        print("instance method")
    }
    
    // 타입 메서드 - static을 사용한다.
    static func typeMethod() {
        print("type method")
    }
}
  • 인스턴스
// 가변 인스턴스
var mutable: Sample = Sample()

// 인스턴스 내부 값 변경.
mutable.mutableProperty = 200
// 불변 프로퍼티는 인스턴스 생성 후 수정할 수 없다.
//mutable.immutableProperty = 200

// 불변 인스턴스
let immutable: Sample = Sample()

// 불변 인스턴스는 아무리 가변 프로퍼티라도 인스턴스 생성 후에 수정할 수 없다.
//immutable.immutableProperty = 200
  • 타입 프로퍼티 및 메서드
    - 구조체 타입 자체가 사용할 수 있는 프로퍼티 및 메서드
//Sample이라는 타입 자체가 사용하는 메서드 & 프로퍼티
Sample.typeProperty = 300
Sample.typeMethod() // type method

// 인스턴스에서는 타입 프로퍼티나 타입 메서드를 사용할 수 없다.
//mutable.typeProperty = 400
//mutable.typeMethod()

예시 (학생 구조체)

struct Student {
    // 가변 프로퍼티
    var name: String = "unknown"
    
    // 키워드도 `로 묶어주면 이름으로 사용할 수 있다 - class라는 키워드가 존재하기때문에 ` `를 사용하였다.
    var `class`: String = "Swift"
    
    // 타입 메서드
    static func selfIntroduce() {
        print("학생타입입니다")
    }
    
    // 인스턴스 메서드
    // self는 인스턴스 자신을 지칭하며, 몇몇 경우를 제외하고 사용은 선택사항입니다
    func selfIntroduce() {
        print("저는 \(self.class)반 \(name)입니다")
    }
}

// 타입 메서드 사용
Student.selfIntroduce() // 학생타입입니다

// 가변 인스턴스 생성
var yagom: Student = Student()
yagom.name = "yagom"
yagom.class = "스위프트"
yagom.selfIntroduce()   // 저는 스위프트반 yagom입니다

// 불변 인스턴스 생성
let jina: Student = Student()

// 불변 인스턴스이므로 프로퍼티 값 변경 불가
// 컴파일 오류 발생
//jina.name = "jina"
jina.selfIntroduce() // 저는 Swift반 unknown입니다

  1. 클래스
  • 클래스는 참조 타입이다.
  • 타입이름은 대문자 카멜케이스를 사용하여 정의한다.
  • 다중상속이 되지 않는다.
  • 구성
class <#이름#> {
   <#구현부#>
}
  • 프로퍼티 및 메서드
    - 구조체와 크게 다른것이 없다. (struct와 유사)
class Sample {
    // 가변 프로퍼티
    var mutableProperty: Int = 100
    
    // 불변 프로퍼티
    let immutableProperty: Int = 100
    
    // 타입 프로퍼티
    static var typeProperty: Int = 100
    
    // 인스턴스 메서드
    func instanceMethod() {
        print("instance method")
    }
    
  • 타입 메서드
    • 앞에 어떤것이 붙냐(static / class)에 따라 재정의가 가능하거나 불가능하다.
    // 재정의 불가 타입 메서드 - static
    static func typeMethod() {
        print("type method - static")
    }
    
    // 재정의 가능 타입 메서드 - class
    class func classMethod() {
        print("type method - class")
    }
}
  • 클래스 사용
// 인스턴스 생성 - 참조정보 수정 가능
var mutableReference: Sample = Sample()

mutableReference.mutableProperty = 200

// 불변 프로퍼티는 인스턴스 생성 후 수정할 수 없다
//mutableReference.immutableProperty = 200


// 인스턴스 생성 - 참조정보 수정 불가
let immutableReference: Sample = Sample()

// 클래스의 인스턴스는 참조 타입이므로 let으로 선언되었더라도 인스턴스 프로퍼티의 값 변경이 가능합니다 (구조체와 다르다!)
immutableReference.mutableProperty = 200

// 다만 참조정보를 변경할 수는 없다.
//immutableReference = mutableReference

// 참조 타입이라도 불변 인스턴스는 인스턴스 생성 후에 수정할 수 없다.
//immutableReference.immutableProperty = 200


// 타입 프로퍼티 및 메서드
Sample.typeProperty = 300
Sample.typeMethod() // type method

// 인스턴스에서는 타입 프로퍼티나 타입 메서드를 사용 할 수 없다.
//mutableReference.typeProperty = 400
//mutableReference.typeMethod()

예시 (학생 클래스)

class Student {
    // 가변 프로퍼티
    var name: String = "unknown"
    
    // 키워드도 `로 묶어주면 이름으로 사용할 수 있습니다
    var `class`: String = "Swift"
    
    // 타입 메서드
    class func selfIntroduce() {
        print("학생타입입니다")
    }
    
    // 인스턴스 메서드
    // self는 인스턴스 자신을 지칭하며, 몇몇 경우를 제외하고 사용은 선택사항입니다
    func selfIntroduce() {
        print("저는 \(self.class)반 \(name)입니다")
    }
}

// 타입 메서드 사용
Student.selfIntroduce() // 학생타입입니다

// 인스턴스 생성
var yagom: Student = Student()
yagom.name = "yagom"
yagom.class = "스위프트"
yagom.selfIntroduce()   // 저는 스위프트반 yagom입니다

// 인스턴스 생성
let jina: Student = Student()
jina.name = "jina"
jina.selfIntroduce() // 저는 Swift반 jina입니다
profile
데일리 정리, 하루에 최소 하나의 글은 적도록 하자.

0개의 댓글