구조체는 타입을 정의하는 것
struct Sample {
// MARK 인스턴스 프로퍼티
// 가변 프로퍼티(값 변경 가능)
var mutableProperty: Int = 100
// 불변 프로퍼티(값 변경 불가능)
let immutableProperty: Int = 100
// 타입 프로퍼티(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()
// MARK 불변 인스턴스는 아무리 가변 프로퍼티라도
// 인스턴스 생성 후에 수정할 수 없습니다
// 컴파일 오류 발생
//immutable.mutableProperty = 200
//immutable.immutableProperty = 200
// 타입 프로퍼티 및 메서드
Sample.typeProperty = 300
Sample.typeMethod() // type method
// 인스턴스에서는 타입 프로퍼티나 타입 메서드를
// 사용할 수 없습니다
// 컴파일 오류 발생
//mutable.typeProperty = 400
//mutable.typeMethod()
class Sample {
// 가변 프로퍼티
var mutableProperty: Int = 100
// 불변 프로퍼티
let immutableProperty: Int = 100
// 타입 프로퍼티
static var typeProperty: Int = 100
// 인스턴스 메서드
func instanceMethod() {
print("instance method")
}
// 타입 메서드
// 상속시 재정의 불가 타입 메서드 - 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()
struct는 JSON같은 것을 받아왔을 때 많이 씀(데이터 원본이 변하지 않음)