라벨이 self인 게시물 표시

[swift] Self VS .self

이미지
[swift] Self VS .self 코딩을 하다 보니, Self.name 을 쓸 때고 있고, leeo.self도 있어서 self라는 키워드는 언제 어떻게 써야하는지에 대해 정리해 놓겠습니다. Self Self 라는 키워드 는 자기 자신의 동적 클래스를 가리킵니다. 아래의 코드를 보시면 Self는 클래스 자기 자신을 가리켜 Self. 으로 인스턴스의 프로퍼티나 메소드에 접근할 수 있는 것을 볼 수 있습니다. class Student { class var name: String { return "Leeo" } func printName() { print(Self.name) } } class Leeo: Student { override class var name: String { return "hyunho" } } let student = Student() student.printName() let leeo = Leeo() leeo.printName() .self .self 를 이해하려면 Metatype에 대해서 알아야 합니다. 이 글의 주제는 아니기 때문에 .self를 언제 사용하는지에 대해서만 간단히 정리하겠습니다. 아래의 결과가 모두 이해된다면 당신은 이미 .self에 대하여 알고 있는 것 입니다! 아니라면 Metatype에 대해서도 공부해 보시길 권해드립니다. let word = "testString" print(word is String) // true print(word is String.Type) // false print(type(of: word) is String.Type) // true print(type(of: word) == String.self) // true //print(type(of: