Swift class & struct

์ตœ์ค€์˜ยท2022๋…„ 7์›” 16์ผ
1

Swift

๋ชฉ๋ก ๋ณด๊ธฐ
5/6
post-custom-banner

ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด ๊ธฐ๋ณธ

์ •์˜

struct PersonStruct {
    var name: String
    var age: Int
    //class์™€ ๋‹ค๋ฅด๊ฒŒ struct๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฉค๋ฒ„์™€์ด์ฆˆ ์ด๋‹ˆ์…œ๋ผ์ด์ €๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
}


class PersonClass {
    var name: String
    var age: Int
    //๋ฉค๋ฒ„์™€์ด์ฆˆ ์ด๋‹ˆ์…œ๋ผ์ด์ €
    init(name: String, age: Int){ 
        self.name = name
        self.age = age
    }
}

๐Ÿ’กํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋“ค์€ ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™” ๋˜์–ด์•ผ ํ•œ๋‹ค.

ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด์˜ ์ฐจ์ด

ํ• ๋‹น ๋ฐฉ์‹์˜ ์ฐจ์ด

var struct1: PersonStruct = PersonStruct(name: "choi", age: 22);
let class1: PersonClass = PersonClass(name: "hoon", age: 21);

var struct2: PersonStruct = struct1;
let class2: PersonClass = class1;

//๊ตฌ์กฐ์ฒด๋Š” ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ์ „๋‹ฌํ•œ๋‹ค.
struct2.name="jun";
print(struct1.name) //choi, ๊ตฌ์กฐ์ฒด๋Š” ๊ฐ๊ฐ์ด ์กด์žฌํ•œ๋‹ค.

//ํด๋ž˜์Šค๋Š” ๊ฐ’์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค์˜ ์œ„์น˜๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
class1.age = 32
print(class2.age) //32, ๊ฐ™์€๊ณณ์„ ์ฐธ์กฐํ•จ์œผ๋กœ ํ•จ๊ป˜ ๋ณ€ํ•œ๋‹ค

//ํด๋ž˜์Šค ์ฐธ์กฐ ์‹๋ณ„ ์—ฐ์‚ฐ์ž ===
print(class1 === class2) //true

์ „๋‹ฌ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ์„œ ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด๊ฐ€ ์ „๋‹ฌ๋ ๋•Œ ์—ญ์‹œ ๊ฐ™์€ ๊ฐœ๋…์ด ์ ์šฉ๋œ๋‹ค.
๐Ÿ’กinout๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊ตฌ์กฐ์ฒด๋„ ์ฐธ์กฐ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

func referStruct(_ refer: inout PersonStuct) -> Void {
    refer.name = "change";
}
func referClass(_ refer: PersonClass) -> Void {
    refer.name = "change";
}

referStruct(&struct1); //inout ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ๋ฐฉ์‹
referClass(class1);

print(struct1.name) //change
print(class1.name) //change

๊ตฌ์กฐ์ฒด์˜ ์‚ฌ์šฉ์‹œ๊ธฐ

  • ๊ฐ’์˜ ์ง‘ํ•ฉ์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ๋งŒ์ด ๋ชฉ์ ์ผ๋•Œ
  • ์บก์Šํ™”ํ•œ ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹จ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋‹นํ•  ๋•Œ
  • ๋‹ค๋ฅธํƒ€์ž…์˜ ์ƒ์†์ด ๋ถˆํ•„์š”ํ•  ๋•Œ

ํ”„๋กœํผํ‹ฐ

์ง€์—ฐ์ €์žฅํ”„๋กœํผํ‹ฐ

๐Ÿ”Ž์‚ฌ์šฉ์‹œ๊ธฐ

  • ์ธ์Šคํ„ด์Šค์˜ ๋งŽ์€์–‘์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•œ๋ฒˆ์— ์ƒ์„ฑ๋˜๋Š”๊ฒŒ ๋น„ํšจ์œจ์ ์ผ ๊ฒฝ์šฐ
  • ๋ชจ๋“ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
//lazy ํ‚ค์›Œ๋“œ๋ฅผ ํ”„๋กœํผํ‹ฐ ์•ž์— ์ž‘์„ฑํ•ด์ค€๋‹ค.
struct LazyStruct {
    lazy var lazyVar: String = "lazy man"
}

๐Ÿ˜ฒ์˜ˆ์™ธ์‚ฌํ•ญ: ์ง€์—ฐ์ €์žฅํ”„๋กœํผํ‹ฐ๊ฐ€ let์œผ๋กœ ์„ ์–ธ๋ฌ์„ ๊ฒฝ์šฐ

struct LazyStruct {
    lazy let lazyVar: String = "lazy man"
}

let lazyClass: LazyStruct = LazyStruct(); 
print(lazyClass.lazyVar); //cannot use mutating getter on immutable value

lazyClass.lazyVar๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ lazyVar์— ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.
let ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ํ”„๋กœํผํ‹ฐ๋Š” ์ดˆ๊ธฐํ™” ์‹œ์—๋งŒ ๊ฐ’ ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•จ์œผ๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ“ƒ๊ณ ๋ ค์‚ฌํ•ญ

  • ๋‹ค์ค‘์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ํ•œ๊บผ๋ฒˆ์— ์ง€์—ฐ์ €์žฅํ”„๋กœํผํ‹ฐ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋ฉด
    ๊ฐ’์ด ์—ฌ๋Ÿฌ๋ฒˆ ํ• ๋‹น๋˜๋Š” ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค.
  • ์ „์—ญ ๋ณ€์ˆ˜/์ƒ์ˆ˜๋Š” ํ˜ธ์ถœ์‹œ์— ํ• ๋‹น์„ ์‹ค์‹œํ•œ๋‹ค.
    ์ฆ‰, ์ „์—ญ๋ณ€์ˆ˜/์ƒ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ lazy ํ‚ค์›Œ๋“œ ํŠน์„ฑ์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋‹ค.

์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ

  • ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ์ ‘๊ทผ์ž์™€ ์„ค์ •์ž๋ฅผ ๊ฐ€๋…์„ฑ ๋†’๊ฒŒ ํ‘œํ˜„๊ฐ€๋Šฅํ•˜๋‹ค.

์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ์™€ ํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž

struct MyInform {
    var name: String
    //์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ sayHello
    var sayHello: String {
        get {
            name + "์ž…๋‹ˆ๋‹ค" //์ฝ”๋“œ๊ฐ€ ํ•œ์ค„์ด๋ผ๋ฉด returnํ‚ค์›Œ๋“œ ์ƒ๋žต๊ฐ€๋Šฅ
        }

        set(newName) { //์ „๋‹ฌ์ธ์ž๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด newValue๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
            name = newName;
        }
    }

    //age ํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž
    var age: Int { 
        //๊ฐ’ ๋ณ€๊ฒฝ ์ง์ „ ์‹คํ–‰๋œ๋‹ค.
        //์ „๋‹ฌ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด newValue, oldValue๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
        willSet(newOne) {
            print("age๊ฐ’์ด \(newOne)๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.");
        }
        //๊ฐ’ ๋ณ€๊ฒฝ ํ›„ ์‹คํ–‰๋œ๋‹ค.
        didSet(oldOne) {
            print("age๊ฐ’์ด \(oldOne)์—์„œ \(age)๋กœ ๋ฐ”๊ผˆ์Šต๋‹ˆ๋‹ค.");
        }
    }
}

var myName: MyInform = MyInform(name: "choi", age: 22);
print(myName.sayHello);   // choi์ž…๋‹ˆ๋‹ค (get)
myName.sayHello = "hoon"; // (set)
print(myName.sayHello);   // hoon์ž…๋‹ˆ๋‹ค (get)


myName.age = 44; //age๊ฐ’์ด 44๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.(willSet) age๊ฐ’์ด 22์—์„œ 44๋กœ ๋ฐ”๊ผˆ์Šต๋‹ˆ๋‹ค.(didSet)

๐Ÿ’กํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋ ๋•Œ๋Š” ํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž ๋งค์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.๐Ÿ˜ฎ

๐Ÿ”Žํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž, ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋Š” ํ”„๋กœํผํ‹ฐ์— ์“ฐ์˜€์ง€๋งŒ ํ•ด๋‹น๊ธฐ๋Šฅ์€
๋ชจ๋“  ์ „์—ญ, ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


ํƒ€์ž…ํ”„๋กœํผํ‹ฐ

c , cpp ์–ธ์–ด์˜ static๊ณผ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ด๋‹ค.

struct TypeStruct {
    static let myStaticProp = 100;

    var calcProp: Int {
        get {
            Self.myStaticProp //๋Œ€๋ฌธ์ž Self๋Š” ๋‚ด๋ถ€์—์„œ ํƒ€์ž…์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        }
    }
}

//ํƒ€์ž…์„ ํ†ตํ•ด ์ง์ ‘ ํ˜ธ์ถœ
print(TypeStruct.myStaticProp) //100

var ts: TypeStruct = TypeStruct();
print(ts.calcProp); //100

๐Ÿ’กself๋Š” ๋‚ด๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  Self๋Š” ๋‚ด๋ถ€์—์„œ ํƒ€์ž…์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.


Self & self

  • self๋Š” ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ์•”์‹œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.
    ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์œผ๋กœ, ํด๋ž˜์Šค๋‚˜ ๊ตฌ์กฐ์ฒด ๋“ฑ์˜ ์ธ์Šคํ„ด์Šค ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ตฌ์กฐ์ฒด๋‚˜ ์—ด๊ฑฐํ˜•์ผ ๊ฒฝ์šฐ, self ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ต์ฒด(์น˜ํ™˜)๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
    ํด๋ž˜์Šค๋Š” ์ฐธ์กฐ ํƒ€์ž…์ด๋ผ, self ํ”„๋กœํผํ‹ฐ์— ๋‹ค๋ฅธ ์ฐธ์กฐ๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

  • Self๋Š” ํƒ€์ž…์ด๋‹ค. ํŠน์ •ํ•œ ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ, ํƒ€์ž…์˜ ์ด๋ฆ„์„ ๋ฐ˜๋ณตํ•ด ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ 
    ํ˜„์žฌ ํƒ€์ž…์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค


ํ”„๋กœํผํ‹ฐ์™€ ํ‚ค๊ฒฝ๋กœ

  • swift์—์„œ ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜/์ƒ์ˆ˜์— ์ €์žฅํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ
    ํด๋ž˜์Šค๋‚˜ ๊ตฌ์กฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์œ„์น˜๋ฅผ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ‚ค๊ฒฝ๋กœ๋Š”
    ๊ตฌ์กฐ์ฒด์˜ ๊ฒฝ์šฐ WritableKeyPath<AddressStruct, String>๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ReferenceWritableKeyPath<AddressClass, String>๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
struct AddStruct {
    var home: String
    var email: String
}

class AddClass {
    var home: String = ""
    var email: String = ""
}

// \, . ๋ฅผ ์ด์šฉํ•ด ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
var structKeyPath = \AddStruct.home;
var classKeyPath = \AddClass.home;

//๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค์˜ ํ‚ค๊ฒฝ๋กœ๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.
print(type(of: structKeyPath)) //WritableKeyPath<AddStruct, String>
print(type(of: classKeyPath)) //ReferenceWritableKeyPath<AddClass, String>

var structInstance: AddStruct = AddStruct(home: "Seoul", email: "email@naver.com");

//ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์— ํ‚ค๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•ด ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
print( structInstance[keyPath: structKeyPath] ); //Seoul

//ํ•ด๋‹นํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์ˆ˜์ธ ๊ฒฝ์šฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
structInstance[keyPath: structKeyPath] = "Daejun";
print( structInstance[keyPath: structKeyPath] ); //Daejun

//.self ๋ฅผ ์‚ฌ์šฉํ•ด ํƒ€์ž…์„ ์œ ์ถ”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
print( structInstance[keyPath: \.self.email ] ) //email@naver.com

๐Ÿ“์œ„๋‚ด์šฉ์ค‘ [keyPath:]์˜ ์‚ฌ์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ํฌ์ŠคํŒ…์—์„œ ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.


๋งค์„œ๋“œ

struct MyStruct {
    var name: String

    //๊ตฌ์กฐ์ฒด์˜ ๊ฒฝ์šฐ ๋‚ด๋ถ€ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ(mutate)ํ•˜๋Š” ๋งค์„œ๋“œ๋Š” mutating ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค˜์•ผ ํ•œ๋‹ค.
    //ํด๋ž˜์Šค๋Š” ์ƒ๊ด€์—†๋‹ค.
    mutating func changeName(_ name: String) {.
        self.name = name; //mutating
    }

    mutating func allnewStruct(_ name: String) {
        //๊ตฌ์กฐ์ฒด์˜ ๊ฒฝ์šฐ๋งŒ self๋ฅผ ์‚ฌ์šฉํ•ด ์ž๊ธฐ์ž์‹ ์„ ์น˜ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. 
        self = MyStruct(name: name);
    }

    //์ธ์Šคํ„ด์Šค ํ•จ์ˆ˜์ฒ˜๋Ÿผ ํ˜ธ์ถœ ํ• ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.
    func callAsFunction() {
        print(name);
    }
    
    //๊ฐœ์ˆ˜์ œํ•œ ์—†์ด ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค.
    func callAsFunction(_ remark: String) {
        print("my name is \(name), \(remark)");
    }
}

var myName: MyStruct = MyStruct(name: "choi");
myName() //choi
myName.changeName("hoon")
myName() //hoon
myName.allnewStruct("young")
myName() //young

myName("nice to meet you.") //my name is young, nice to meet you.

ํƒ€์ž…๋งค์„œ๋“œ

  • ์•ž์˜ ํƒ€์ž… ํ”„๋กœํผํ‹ฐ์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์ด๋‹ค.
  • class ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ํƒ€์ž…๋งค์„œ๋“œ๋Š” ์ƒ์†์‹œ ์žฌ์ •์˜(overriding)๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์žฌ์ •์˜๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
class Aclass {
    static func sayHello(){
        print("Hello~")
    }

    class func sayGoodBye() {
        print("Good Bye")   
    }

    static func hiAndBye() {
        //ํƒ€์ž… ๋งค์„œ๋“œ์—์„œ self๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ํƒ€์ž…์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        self.sayHello();
        self.sayGoodBye();
    }
}

class Bclass: Aclass{
    //ํƒ€์ž…๋งค์„œ๋“œ ์žฌ์ •์˜
    override class func sayGoodBye(){
        print("Good Bye, see you again!")
    }
}

Aclass.hiAndBye(); //Hello~ Good Bye

//์žฌ์ •์˜ ๋ฌ์Œ์œผ๋กœ ์žฌ์ •์˜์ „ ๋งค์„œ๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
Bclass.sayGoodBye(); //Good Bye, see you again!

์ด๋‹ˆ์…œ๋ผ์ด์ €

  • ์˜ต์…”๋„ ํƒ€์ž…์„ ์ œ์™ธํ•œ ์ธ์Šคํ„ด์Šค์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋Š” ์ดˆ๊ธฐํ™”์‹œ ๊ฐ’์„ ๊ฐ€์ ธ์•ผํ•œ๋‹ค.
  • ์ƒ์ˆ˜ ํ”„๋กœํผํ‹ฐ์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ด๋‹ˆ์…œ๋ผ์ด์ €์—์„œ ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

์‹คํŒจ๊ฐ€๋Šฅํ•œ ์ด๋‹ˆ์…œ๋ผ์ด์ €

  • ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”์‹œ ์ „๋‹ฌ์ธ์ž์˜ ์˜ค๋ฅ˜ ๋“ฑ์œผ๋กœ ์˜๋„์น˜์•Š์€ ์ƒ์„ฑ์„ ๋ง‰๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
  • init ํ•จ์ˆ˜ ๋’ค์— ?๋ฅผ ๋ถ™์—ฌ ์„ ์–ธํ•œ๋‹ค.
  • ์ดˆ๊ธฐํ™”์— ์‹คํŒจํ•œ๊ฒฝ์šฐ nil์„ ๋ฐ˜ํ™˜ํ•ด ์‹คํŒจ์‚ฌ์‹ค์„ ์•Œ๋ฆฐ๋‹ค.
struct School {
    var name: String;

    init?(_ name: String){
        if name.isEmpty {
            return nil;
        }
        self.name = name;
    }
} 

var mySchool: School? = School("")
print(mySchool) //nil

//์—ด๊ฑฐํ˜• ํ™œ์šฉ
enum SchoolAge: String{
    case elementary="์ดˆ๋“ฑํ•™์ƒ"
    case middle="์ค‘ํ•™์ƒ"

    init?(_ age: Int){
        switch(age){
            case 8...13:
                //self๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹  ์ง€์ •
                self = .elementary
            case 14...16:
                self = .middle
            default:
                return nil
        }
    }
}

//์ดˆ๊ธฐํ™” ์‹คํŒจ์‹œ ์˜ต์…”๋„๊ฐ’์„ ๋ฐ›์•„์•ผ ํ•จ์œผ๋กœ ์˜ต์…”๋„ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.
var choi: SchoolAge? = SchoolAge(22);
print(choi == Optional<SchoolAge>.none); //true

๐Ÿ’ก์—ด๊ฑฐํ˜• ํ•ญ๋ชฉ์„ rawValue๋กœ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์‹คํŒจ๊ฐ€๋Šฅํ•œ ์ด๋‹ˆ์…œ๋ผ์ด์ €์˜ ์˜ˆ์‹œ์ด๋‹ค.

enum ForSearch: Int{
    case zero, one
    
    init?(value: Int){
        switch value {
        case 0:
            self = .zero
        case 1:
            self = .one
        default:
            return nil;
        }
    }
}

//value๋ฅผ rawValue๋กœ ๋ฐ”๊พธ๊ธฐ๋งŒ ํ•˜๋ฉด ๊ธฐ์กด ๋งค์„œ๋“œ๋ž‘ ์ผ์น˜ํ•œ๋‹ค.
var test: ForSearch? = ForSearch(value: 2)

print(test); //nil

ํด๋กœ์ €๋ฅผ ํ™œ์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•

์ดˆ๊ธฐํ™” ํด๋กœ์ € ๋‚ด๋ถ€ ์œ ์˜์‚ฌํ•ญ

  • ์ธ์Šคํ„ด์Šค์˜ ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ, ๋งค์„œ๋“œ๊ฐ€ ์ดˆ๊ธฐํ™” ์ „์ž„์œผ๋กœ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
  • self ํ”„๋กœํผํ‹ฐ ์—ญ์‹œ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด ํ”„๋กœํผํ‹ฐ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
struct ClosureStruct {
    var name: String = {
        () -> String in //์ƒ๋žต๊ฐ€๋Šฅํ•œ ํ‘œํ˜„
        return "choi"  //ํ”„๋กœํผํ‹ฐ ํƒ€์ž…์ธ String
    }() //ํด๋กœ์ €๋ฅผ ์‹คํ–‰๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ์œผ๋กœ ()๋ถ™์—ฌ ์‹คํ–‰๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.
}

var me: ClosureStruct = ClosureStruct();
print(me.name); //choi

๐Ÿ”Žํด๋กœ์ €๋Š” ํด๋กœ์ € ํฌ์ŠคํŒ…์—์„œ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.


์ธ์Šคํ„ด์Šค ์†Œ๋ฉธ

  • ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์—์„œ๋งŒ ๊ตฌํ˜„๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์‚ฌ์šฉ๋ชฉ์ ์€ ์˜ˆ๋ฅผ๋“ค์–ด ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ค ํŒŒ์ผ์„ ์—ด๊ณ  ์†Œ๋ฉธ๋œ๋‹ค๋ฉด ํŒŒ์ผ์€ ์—ด๋ฆฐ์ฑ„๋กœ ๋ฐฉ์น˜๋œ๋‹ค.
    ์ด๋Ÿฐ ์ƒํ™ฉ์„ ๋ง‰๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • deinit ์ด๋ฆ„์œผ๋กœ ์„ ์–ธ๋˜๋ฉฐ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
class SomeClass{
    deinit {
        print("good bye");
    }
}

var test: SomeClass? = SomeClass();

//์ธ์Šคํ„ด์Šค๊ฐ€ ์–ด๋Š๊ณณ์—๋„ ์ฐธ์กฐ๋˜์žˆ์ง€ ์•Š์Œ์œผ๋กœ ์†Œ๋ฉธ๋œ๋‹ค.๐Ÿ‘‰deinitํ˜ธ์ถœ
test = nil; //good bye

profile
Developer๊ฐ€ ๋˜๊ณ ์‹ถ์€ ๊ฟˆ๋‚˜๋ฌด
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€