enum์ static ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๊ด๋ฆฌ๋ฅผ ํ๋ค๊ฐ ๋ฌธ๋ ์ด๋ฐ ์๊ฐ์ด ๋ค์๋ค. Type property์๋ 2๊ฐ์ง ์ข
๋ฅ, static
, class
๊ฐ ์๋๋ฐ ๋ญ๊ฐ ๋ค๋ฅธ๊ฑฐ์ง? ์ด์ฐธ์ ํท๊ฐ๋ ธ๋ ์ฉ์ด๊น์ง ๋ชฝ๋
์ ๋ฆฌํด๋ณด๋ ค ํ๋ค.
์ผ๋จ static
, class
์ ๋ํด์ ์๊ฐํด๋ณด๋ฉด ๋๊ฐ์ง ์ํฉ์์ ์ฌ์ฉํ ์ ์๋ค. method์ ๊ฒฝ์ฐ์ property์ ๊ฒฝ์ฐ์ด๋ค. ๊ฐ๊ฐ ์ค๋ช
ํด๋ณด๊ฒ ๋ค.
static, class Method
override ๊ฐ๋ฅ ์ฌ๋ถ์ ์ฐจ์ด
๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด, static
์ ๊ฒฝ์ฐ override
๊ฐ ๋ถ๊ฐํ๊ณ , class
์ ๊ฒฝ์ฐ ๊ฐ๋ฅํ๋ค. ๊ทธ๋ ๋ค๋ ์๊ธฐ๋ static
๊ณผ class
ํค์๋์ ๊ฒฝ์ฐ struct, enum์์๋ ๊ฐ์ ๋์์ ํ๋ค๋ ๊ฒ์ด๋ค. ์๋ํ๋ฉด ์ ์ด์ struct์ enum์ override๊ฐ ๋ถ๊ฐํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋๊ฐ์ผ๋ฉด static์ ์ฌ์ฉํ๊ฒ ๋ค.
class SampleClass {
static func staticFunction() {
print("๋ static function์ด์")
}
class func classFunction() {
print("๋ class function์ด์")
}
}
struct SampleStruct {
// Working
static func staticFunction() {
print("๋ static function์ด์")
}
// Not Working: Class methods are only allowed within classes; use 'static' to declare a static metho
class func classFunction() {
print("๋ class function์ด์")
}
}
enum SampleEnum {
// Working
static func staticFunction() {
print("๋ static function์ด์")
}
// Not Working: Class methods are only allowed within classes; use 'static' to declare a static metho
class func classFunction() {
print("๋ class function์ด์")
}
}
์ค ์ญ์๋, ์ ํ์ด๋ค. ์์ ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅํ ๋
์, ์ฆ struct, enum์์ class
ํค์๋๋ฅผ ์ฌ์ฉํ ๋ฉ์๋๋ฅผ ๋ง๋ค๋ ค๊ณ ํ๋ compile ์๋ฌ๊ฐ ๋ฌ๋ค. ์ข๋ค. class์์๋ง class
keyword๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๊ทธ๋ฌ๋ฉด class ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ ๋ง๋ก override๊ฐ ๊ฐ๋ฅํ ๊น?
class SampleClass {
// Working
static func staticFunction() {
print("๋ static function์ด์")
}
// Not Working
class func classFunction() {
print("๋ class function์ด์")
}
}
class SubSmapleClass: SampleClass {
override static func staticFunction() {
// Compile Error!: Method does not override any method from its superclass
}
override class func classFunction() {
// No Error!
}
}
์ค์ ๋ก ์๋ฌ๊ฐ ๋์ง ์๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
final keyword
ํ ๊ทธ๋ฌ๋ฉด class function์ final keyword๋ฅผ ์ ์ด์ override๊ฐ ๋์ด์ ๋์ง ์๋๋ค๋ ๊ฒ์ ๋ช ์ํ๋ฉด ํด๊ฒฐ๋๋ ๋ฌธ์ ์๋๊ฐ? ๋ง๋ค. ์ทจํฅ ์ฐจ์ด๋ค. ๊ทธ๋ฐ๋ฐ ๋๊ฐ์ผ๋ฉด ์์ธ ๊ฒ ๊ฐ๋ค. ์ผ๋จ ์๊ณ ๋ ์์.
class SampleClass {
class final func classFunction() {
print("๋ class function์ด์")
}
}
class SubSmapleClass: SampleClass {
override class func classFunction() {
// Compile Error!: Class method overrides a 'final' class method
}
}
static, class Property
์ญ์๋ override ๊ฐ๋ฅ ์ฌ๋ถ์ ์ฐจ์ด
์ญ์๋ ๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด ๊ฐ๋ค. ํ์ง๋ง ์กฐ๊ธ ์ ํํ ์ ํ์๊ฐ ์๋ค. ์ผ๋จ Property๋ฅผ ์ด๋ป๊ฒ ๊ตฌ๋ถํ๋์ง๋ถํฐ ์์๋ณด์.
Property๋ฅผ ๋
ผ๋ฆฌ์ ์ผ๋ก ๊ตฌ๋ถํด๋ณด๋ฉด ์์ ๊ฐ๋ค. ๋ณ๊ฒฝ ์ฌ๋ถ๋ let
, var
๋ฅผ ๋งํ๋ค. ๊ฐ๋จํ๊ธฐ ๋๋ฌธ์ ๊ฒฝ์ฐ์ ์ ์กฐํฉ์์๋ ์ ์ธํ๋๋ก ํ๊ฒ ๋ค. ์ฌ๊ธฐ์, ์ด ์กฐํฉ์ผ๋ก ๋ง๋ค์ด์ง๋ ๋
์๋ค์ ๋ง๋ค์ด๋ณด๋ฉด ์ด๋ ๊ฒ ๋๋ค.
- Instance Stored Property
- Instance Computed Property
- Type Stored Property: ์ฃผ์ธ๊ณต
- Type Computed Property: ์ฃผ์ธ๊ณต
์ด ๊ฐ๊ฐ์ ๋ํด ๊ฐ๋จํ๊ฒ ์์๋ณด๋ฉด์ ์ดํดํด ๋ณผ ๊ฒ์ด๋ค.
Instance Stored Property
class SampleClass {
let storedImmutableProperty: String = "์ธ์คํด์ค ์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
var storedMutableProperty: String = "์ธ์คํด์ค ์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
}
struct SampleStruct {
let storedImmutableProperty: String = "์ธ์คํด์ค ์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
var storedMutableProperty: String = "์ธ์คํด์ค ์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
}
Instance์ โ์ ์ฅโ ๋๋ ๋ ์์ด๋ค. ๋ณดํต ์ฐ๋ฆฌ๊ฐ ๋ง์ด ์ฌ์ฉํ๋ ๋ ์์ด๋ผ ์๊ฐํ๋ฉด ๋๋ค.
Instance Computed Property
class SampleClass {
// Not Working
let computedImmutablePropertyNOTWORKING: String { // ๊ณ์ฐ ํ๋กํผํฐ๋ let์ ์ฌ์ฉํ ์ ์๋ค.
return "์ธ์คํด์ค ๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
var computedMutableProperty: String {
"์ธ์คํด์ค ๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
}
struct SampleStruct {
// Not Working
let computedImmutablePropertyNOTWORKING: String { // ๊ณ์ฐ ํ๋กํผํฐ๋ let์ ์ฌ์ฉํ ์ ์๋ค.
"์ธ์คํด์ค ๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
var computedMutableProperty: String {
"์ธ์คํด์ค ๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
}
๊ณ์ฐ ํ๋กํผํฐ์ ๊ฒฝ์ฐ์๋ ์ผ์ข ์ ํจ์๋ผ ์๊ฐํด๋ ๋ฌด๋ฐฉํ๋ค. ํธ์ถ๋๋ ์๊ฐ ๊ฐ์ด ๊ณ์ฐ๋์ด ๋ฆฌํดํ๋ค. ์ด ๊ณผ์ ์์ instance์ ๊ณ์ํด์ ๋จ์์์ง ์๊ธฐ ๋๋ฌธ์ computed๋ผ ๋ถ๋ฅธ๋ค.
ํ์ง๋ง ์ฃผ์ํ ์ ์ด ์๋๋ฐ, computed property์ ๊ฒฝ์ฐ let
์ ์ฌ์ฉํ ์ ์๋ค.
Type Stored Property
์ด์ ๋๋์ด ์ฐ๋ฆฌ๊ฐ ์๊ณ ์ถ์ ๋ ์๋ค์ด ๋์จ๋ค.
class SampleClass {
// static: Working
static let staticStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
static var staticStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
// class: Not working
// Error: Class stored properties not supported in classes;
class let classStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
class var classStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
}
struct SampleStruct {
// static: Working
static let staticStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
static var staticStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
// class: Not working
// Error: Class properties are only allowed within classes; use 'static' to declare a static property
class let classStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
class var classStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
}
enum SampleEnum {
// static: Working
static let staticStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
static var staticStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
// class: Not working
// Error: Class properties are only allowed within classes; use 'static' to declare a static property
class let classStoredImmutableProperty: String = "ํ์
์ ์ฅ ๋ถ๋ณ ํ๋กํผํฐ"
class var classStoredMutableProperty: String = "ํ์
์ ์ฅ ๊ฐ๋ณ ํ๋กํผํฐ"
}
์ผ๋จ ์ ์ ํ์
์ฆ, static
, class
์ ๊ฒฝ์ฐ struct, class, enum ๋ชจ๋์์ ์ ์ธ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์, ๋ชจ๋ ์ ์ด๋ณด์๋ค. ์ ๊ธฐํ ๊ฒฐ๊ณผ๊ฐ ๋์๋๋ฐ, class์์๋ class
ํค์๋๋ก Type Stored property๋ฅผ ๋ง๋ค ์ ์์๋ค. static
ํค์๋๋ก๋ง ์์ฑ์ด ๊ฐ๋ฅํ๋ค. ๋ค์์ ๋์ค๊ฒ ์ง๋ง computed property์ ๊ฒฝ์ฐ class์์ class
ํค์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
๋ํ class
๋ผ๋ ์ด๋ฆ ๋ต๊ฒ, struct, enum์์๋ ์์ ์ฌ์ฉ์ด ๋ถ๊ฐํ๋ค. ์ญ์ ๋ง์๋๋๊ฒ ์ข์ ํ๋จ์ธ ๋ฏ ํ๋ค.
Type Computed Property
class SampleClass {
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let์ด ๋ถ๊ฐํจ
static let staticComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
static var staticComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let์ด ๋ถ๊ฐํจ
class let classComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
class var classComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
}
struct SampleStruct {
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let์ด ๋ถ๊ฐํจ
static let staticComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
static var staticComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let์ด ๋ถ๊ฐํจ, class keyword๋ class์์๋ง ์ฌ์ฉ๊ฐ๋ฅํจ
class let classComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Not Working: struct์์๋ class type์ ์ฌ์ฉํ ์ ์์
class var classComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
}
enum SampleEnum {
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let ์ฌ์ฉ์ด ๋ถ๊ฐํจ
static let staticComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Working
static var staticComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
// Not Working: ๊ณ์ฐ ํ๋กํผํฐ๋ let ์ฌ์ฉ์ด ๋ถ๊ฐํจ
class let classComputedImmutableProperty: String {
return "ํ์
๊ณ์ฐ ๋ถ๋ณ ํ๋กํผํฐ"
}
// Not Working: enum์์๋ class type์ ์ฌ์ฉํ ์ ์์
class var classComputedMutableProperty: String {
return "ํ์
๊ณ์ฐ ๊ฐ๋ณ ํ๋กํผํฐ"
}
}
๊ฑฐ์ ๋ชจ๋ ๋ ์๋ค์ด ํผํฉ๋ ํํ๋ค! ์ด๊ฑธ ์ดํดํ๋ค๋ฉด ๋ฐฉ์์ ๋ชจ๋ ์ดํดํ๋ค๊ณ ๋ณผ ์ ์๋ค.
๋ง๋ฌด๋ฆฌ
class
keyword๋ Class type์์๋ง ์ฌ์ฉํ๋ค. override๊ฐ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ด๋ค.class
keyword๋ฅผ class์์ Type property๋ก ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด computed property๋ง ๊ฐ๋ฅํ๋ค. stored property๋ฅผ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉดstatic
์ ์ฌ์ฉํ์.
์ด๋ ๊ฒ ์ ๋ฆฌํ๊ณ ๋๋ ์ ์ ๋ณ์๋ ์ธ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์์ง์ ๋ํ ๊ณ ๋ฏผ์ด ์๊ฒผ๋ค. ๋ค์์ ์์ฑํด๋ณด๋๋ก ํ๊ฒ ๋ค. ๋!