struct Sample {
// 가변 프로퍼티(값 변경 가능)
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()
// 불변 인스턴스는 아무리 가변 프로퍼티라도
// 인스턴스 생성 후에 수정할 수 없습니다
// 컴파일 오류 발생
//immutable.mutableProperty = 200
//immutable.immutableProperty = 200
// 타입 프로퍼티 및 메서드
Sample.typeProperty = 300
Sample.typeMethod() // type method
// 인스턴스에서는 타입 프로퍼티나 타입 메서드를
// 사용할 수 없습니다
// 컴파일 오류 발생
//mutable.typeProperty = 400
//mutable.typeMethod()
예시)
struct Student {
// 가변 프로퍼티
var name: String = "unknown"
// 키워드도 `로 묶어주면 이름으로 사용할 수 있습니다
var `class`: String = "Swift"
// 타입 메서드
static func selfIntroduce() {
print("학생타입입니다")
}
// 인스턴스 메서드
// self는 인스턴스 자신을 지칭하며, 몇몇 경우를 제외하고 사용은 선택사항입니다
func selfIntroduce() {
print("저는 \(self.class)반 \(name)입니다")
}
}
// 타입 메서드 사용
Student.selfIntroduce() // 학생타입입니다
// 가변 인스턴스 생성
var rin: Student = Student()
rin.name = "rin"
rin.class = "스위프트"
rin.selfIntroduce() // 저는 스위프트반 rin입니다
// 불변 인스턴스 생성
let jina: Student = Student()
// 불변 인스턴스이므로 프로퍼티 값 변경 불가
// 컴파일 오류 발생
//jina.name = "jina"
jina.selfIntroduce() // 저는 Swift반 unknown입니다
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()
예시 )
class Student {
// 가변 프로퍼티
var name: String = "unknown"
// 키워드도 `로 묶어주면 이름으로 사용할 수 있습니다
var `class`: String = "Swift"
// 타입 메서드
class func selfIntroduce() {
print("학생타입입니다")
}
// 인스턴스 메서드
// self는 인스턴스 자신을 지칭하며, 몇몇 경우를 제외하고 사용은 선택사항입니다
func selfIntroduce() {
print("저는 \(self.class)반 \(name)입니다")
}
}
// 타입 메서드 사용
Student.selfIntroduce() // 학생타입입니다
// 인스턴스 생성
var rin: Student = Student()
rin.name = "rin"
rin.class = "스위프트"
rin.selfIntroduce() // 저는 스위프트반 rin입니다
// 인스턴스 생성
let jina: Student = Student()
jina.name = "jina"
jina.selfIntroduce() // 저는 Swift반 jina입니다
enum 이름 {
case 이름1
case 이름2
case 이름3, 이름4, 이름5
// ...
}
// 예제
enum BoostCamp {
case iosCamp
case androidCamp
case webCamp
}
enum Weekday {
case mon
case tue
case wed
case thu, fri, sat, sun
}
// 열거형 타입과 케이스를 모두 사용하여도 됩니다
var day: Weekday = Weekday.mon
// 타입이 명확하다면 .케이스 처럼 표현해도 무방합니다
day = .tue
print(day) // tue
// switch의 비교값에 열거형 타입이 위치할 때
// 모든 열거형 케이스를 포함한다면
// default를 작성할 필요가 없습니다
switch day {
case .mon, .tue, .wed, .thu:
print("평일입니다")
case Weekday.fri:
print("불금 파티!!")
case .sat, .sun:
print("신나는 주말!!")
}
enum Fruit: Int {
case apple = 0
case grape = 1
case peach
// mango와 apple의 원시값이 같으므로
// mango 케이스의 원시값을 0으로 정의할 수 없습니다
// case mango = 0
}
print("Fruit.peach.rawValue == \(Fruit.peach.rawValue)")
// Fruit.peach.rawValue == 2
enum School: String {
case elementary = "초등"
case middle = "중등"
case high = "고등"
case university
}
print("School.middle.rawValue == \(School.middle.rawValue)")
// School.middle.rawValue == 중등
// 열거형의 원시값 타입이 String일 때, 원시값이 지정되지 않았다면
// case의 이름을 원시값으로 사용합니다
print("School.university.rawValue == \(School.university.rawValue)")
// School.middle.rawValue == university
// rawValue를 통해 초기화 한 열거형 값은 옵셔널 타입이므로 Fruit 타입이 아닙니다
//let apple: Fruit = Fruit(rawValue: 0)
let apple: Fruit? = Fruit(rawValue: 0)
// if let 구문을 사용하면 rawValue에 해당하는 케이스를 곧바로 사용할 수 있습니다
if let orange: Fruit = Fruit(rawValue: 5) {
print("rawValue 5에 해당하는 케이스는 \(orange)입니다")
} else {
print("rawValue 5에 해당하는 케이스가 없습니다")
} // rawValue 5에 해당하는 케이스가 없습니다
enum Month {
case dec, jan, feb
case mar, apr, may
case jun, jul, aug
case sep, oct, nov
func printMessage() {
switch self {
case .mar, .apr, .may:
print("따스한 봄~")
case .jun, .jul, .aug:
print("여름 더워요~")
case .sep, .oct, .nov:
print("가을은 독서의 계절!")
case .dec, .jan, .feb:
print("추운 겨울입니다")
}
}
}
Month.mar.printMessage()
struct ValueType {
var property = 1
}
class ReferenceType {
var property = 1
}
// 첫 번째 구조체 인스턴스
let firstStructInstance = ValueType()
// 두 번째 구조체 인스턴스에 첫 번째 인스턴스 값 복사
var secondStructInstance = firstStructInstance
// 두 번째 구조체 인스턴스 프로퍼티 값 수정
secondStructInstance.property = 2
// 두 번째 구조체 인스턴스는 첫 번째 구조체를 똑같이 복사한
// 별도의 인스턴스이기 때문에
// 두 번째 구조체 인스턴스의 프로퍼티 값을 변경해도
// 첫 번째 구조체 인스턴스의 프로퍼티 값에는 영향이 없음
print("first struct instance property : \(firstStructInstance.property)") // 1
print("second struct instance property : \(secondStructInstance.property)") // 2
// 클래스 인스턴스 생성 후 첫 번째 참조 생성
let firstClassReference = ReferenceType()
// 두 번째 참조 변수에 첫 번째 참조 할당
let secondClassReference = firstClassReference
secondClassReference.property = 2
// 두 번째 클래스 참조는 첫 번째 클래스 인스턴스를 참조하기 때문에
// 두 번째 참조를 통해 인스턴스의 프로퍼티 값을 변경하면
// 첫 번째 클래스 인스턴스의 프로퍼티 값을 변경하게 됨
print("first class reference property : \(firstClassReference.property)") // 2
print("second class reference property : \(secondClassReference.property)") // 2