Swift:: 구조체

jahlee·2023년 4월 26일
0

Swift기초

목록 보기
9/26
post-thumbnail

정의 문법

struct Sample {
    // 가변 프로퍼티
    var mutableProperty: Int = 100 

    // 불변 프로퍼티
    let immutableProperty: Int = 100 

    // 타입 프로퍼티
    static var typeProperty: Int = 100 

    // 인스턴스 메서드
    func instanceMethod() {
        print("instance method")
    }

    // 타입 메서드
    static func typeMethod() {
        print("type method")
    }
}

타입 프로퍼티 예제

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

// 인스턴스에서는 타입 프로퍼티나 타입 메서드를
// 사용할 수 없습니다
// 컴파일 오류 발생
// 
//mutable.typeProperty = 400
//mutable.typeMethod()

0개의 댓글