728x90
반응형
import Cocoa
// 10_ Struct
// 구조체
// 구조체는 Swift에서 Type을 정의한다.
// kotlin은 Struct구조이다.
// 2020. 구글스토어 70% 코틀린이다.
// Property & Method
struct Sample {
var sampleProperty: Int = 10 // 가변 Property
let fixedSampleProperty: Int = 100 // 불편 Property
static var typeProperty: Int = 1000 // 타입 Property
func instanceMethod() {
print("instance method")
}
static func typeMethod(){
print("type method")
}
}
// 타입 프로퍼티 및 메서드
var samp: Sample = Sample()
print(samp.sampleProperty)
samp.sampleProperty = 100
print(samp.sampleProperty)
print(samp.fixedSampleProperty)
//samp.fixedSampleProperty = 100
Sample.typeProperty = 300
print(Sample.typeProperty)
Sample.typeProperty
samp.instanceMethod()
// 학생 구조체
struct Student{
var name : String = "unknown"
var `class`: String = "Swift"
static func selfIntroduce() { // 타입 메서드
print("학생 타입 입니다.")
}
func selfIntroduce() { // 둘의 변수 명이 같아도 상관이 없다.
print("저는 \(`class`)반 \(name) 입니다.")
// print("저는 \(self.class)반 \(name) 입니다.") 위에 (`class`)가 안될 떄 쓰는 방법
}
}
// 학생 구조체 사용
Student.selfIntroduce()
// 가변 인스턴스 사용
var student: Student = Student()
student.name = "James"
student.class = "Swift"
student.selfIntroduce()
반응형
'개발자 코드(Code) > Swift(문법)' 카테고리의 다른 글
Swift) 12_열거형 (0) | 2020.09.24 |
---|---|
Swift) 11_클래스 (0) | 2020.09.24 |
Swift) 09_값타입과 참조타입 (0) | 2020.09.24 |
Swift) 08_옵셔널.Playground (0) | 2020.09.24 |
Swift) 07_반복문.Playground (0) | 2020.09.24 |