Swift

[Swift] ์˜ต์…”๋„ : ์˜ต์…”๋„ ์ถ”์ถœ

yevdev 2022. 3. 17. 15:04

๐Ÿ’ก ์˜ต์…”๋„

๋‹จ์–ด ๋œป ๊ทธ๋ž˜๋„ '์„ ํƒ์ ์ธ', ์ฆ‰ ๊ฐ’์ด '์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ์Œ'์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œํ˜„

'๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ๋“ฑ์— ๊ผญ ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค.' ์ฆ‰, ๋ณ€์ˆ˜ ๋˜๋Š” ์ƒ์ˆ˜์˜ ๊ฐ’์ด nil์ผ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ

 

 

์˜ต์…”๋„ ๋ณ€์ˆ˜์˜ ์„ ์–ธ ๋ฐ nil ํ• ๋‹น

var myName: String? = "yejin"
print(myName) // yejin

//์˜ต์…”๋„ ํƒ€์ž…์˜ ๊ฐ’์„ print ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•˜๋ฉด
//Optional("yejin")์ด๋ผ๊ณ  ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ด ์ •์ƒ
//๋‹ค๋งŒ, ์ฐจํ›„์˜ ์ฃผ์„ํ‘œํ˜„์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด์„œ
//Optional()์€ ์ƒ๋žตํ•˜๊ณ  ๊ฐ’๋งŒ ํ‘œ๊ธฐํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.
//๋˜, ์˜ต์…”๋„ ํƒ€์ž…์˜ ๊ฐ’์„ print ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด
//์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
//์ •์ƒ์ด๋ฏ€๋กœ ๋†€๋ผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

myName = nil
print(myName)	// nil

 

์˜ต์…”๋„์€ ์—ด๊ฑฐํ˜•์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

์˜ต์…”๋„ ์—ด๊ฑฐํ˜•์˜ ์ •์˜

public enum Optional<Wrapped> : ExpressibleByNilLiteral {
    case none
    case some(Wrapped)
    public init(_ some: Wrapped)
    /// ์ค‘๋žต...
}

โ—๏ธExpressibleByNilLiteral ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฆ„

โ—๏ธ์˜ต์…”๋„์ด ๊ฐ’์„ ๊ฐ–๋Š” ์ผ€์ด์Šค์™€ ๊ทธ๋ ‡์ง€ ๋ชปํ•œ ์ผ€์ด์Šค ๋‘๊ฐ€์ง€๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

โ—๏ธ์ฆ‰, nil์ผ ๋•Œ๋Š” none ์ผ€์ด์Šค๊ฐ€ ๋  ๊ฒƒ์ด๊ณ , ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” some ์ผ€์ด์Šค๊ฐ€ ๋˜๋Š”๋ฐ, ์—ฐ๊ด€ ๊ฐ’์œผ๋กœ Wrapped์— ๊ฐ’์ด ํ• ๋‹น ๋œ๋‹ค.

โ—๏ธ์ฆ‰, ๊ฐ’์ด ์˜ต์…”๋„์ด๋ผ๋Š” ์—ด๊ฑฐํ˜•์˜ ๋ฐฉํŒจ๋ง‰์— ๋ณดํ˜ธ๋˜์–ด ๋ž˜ํ•‘๋˜์–ด ์žˆ๋Š” ๋ชจ์Šต

 

์˜ต์…”๋„ ์ž์ฒด๊ฐ€ ์—ด๊ฑฐํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ๋ณ€์ˆ˜๋Š” switch ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๊ฐ’์ด ์žˆ๊ณ  ์—†์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ( +where ๋กœ ์—ฌ๋Ÿฌ ์ผ€์ด์Šค ์กฐ๊ฑด ์ถ”๊ฐ€! )

let numbers: [Int?] = [2, nil, -4, nil, 100]

for number in numbers {
    switch number{
    case .some(let value) where value < 0:
        print("Negative value! \(value)")
    case .some(let value) where value > 10:
        print("Large value! \(value)")
        
    case .some(let value):
        print("Value \(value)")
    case .none:
        print("nil")
    }
}

// Value 2
// nil
// Negative value! -4
// nil
// Large value! 100

 

 

 

์˜ต์…”๋„ ์ถ”์ถœ

1๏ธโƒฃ ๊ฐ•์ œ ์ถ”์ถœ : ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์œ„ํ—˜, ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜ ๊ฐ€๋Šฅ์„ฑ ๋งค์šฐ ๋†’์Œ!

var myName: String? = "yejin"

// ์˜ต์…”๋„์ด ์•„๋‹Œ ๋ณ€์ˆ˜์—๋Š” ์˜ต์…”๋„ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค. ์ถ”์ถœํ•ด์„œ ํ• ๋‹นํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
var yejin: String = myName!

myName = nil
yejin = myName! // ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜

// if ๊ตฌ๋ฌธ ๋“ฑ ์กฐ๊ฑด๋ฌผ์„ ์ด์šฉํ•ด์„œ ์กฐ๊ธˆ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
if myName != nil {
    print("My name is \(myName!)")
}
else{
    print("myName === nil")
}

 

 

2๏ธโƒฃ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์˜ต์…”๋„ ๊ฐ’์˜ ์ถ”์ถœ (๊ฐ€์žฅ ์ง€ํ–ฅํ•˜๋Š” ๋ฐฉ๋ฒ•)

๐Ÿ’ก์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ? ์˜ต์…”๋„์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉ

๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์˜ต์…”๋„์—์„œ ์ถ”์ถœํ•œ ๊ฐ’์„ ์ผ์ • ๋ธ”๋ก ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋กœ ํ• ๋‹นํ•ด์„œ ์˜ต์…”๋„์ด ์•„๋‹Œ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ๋” ํ•œ๋‹ค.

์˜ต์…”๋„์€ if ๋˜๋Š” while ๊ตฌ๋ฌธ ๋“ฑ๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

var myName: String? = "yejin"

// ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ†ตํ•œ ์ž„์‹œ ์ƒ์ˆ˜ ํ• ๋‹น
if let name = myName {
    print("My name is \(name)")
} else {
    print("MyName == nil")
}
// My name is yejin

// ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ†ตํ•œ ์ž„์‹œ ๋ณ€์ˆ˜ ํ• ๋‹น
if var name = myName {
    name = "hejin" // ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ๋‚ด๋ถ€์—์„œ ๊ฐ’ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅ
    print("My name is \(name)")
} else {
    print("MyName == nil")
}
// My name is hejin

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์˜ต์…”๋„ ๊ฐ’์˜ ์ถ”์ถœ

๐Ÿšซ ๋ฐ”์ธ๋”ฉํ•˜๋ ค๋Š” ์˜ต์…˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ’์ด ์—†๋‹ค๋ฉด ํ•ด๋‹น ๋ธ”๋ก ๋‚ด๋ถ€์˜ ๋ช…๋ น๋ฌธ์€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

var myName: String? = "yejin"
var yourName: String? = nil

// friend์— ๋ฐ”์ธ๋”ฉ์ด ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
if let name = myName, let yourname = yourName {
    print("We are friend! \(name) & \(yourname)")
}

yourName = "hyejin"

if let name = myName, let yourname = yourName {
    print("We are friend! \(name) & \(yourname)")
}
// We are friend! yejin & hyejin

 

 

3๏ธโƒฃ ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„

- ๋•Œ๋•Œ๋กœ nil์„ ํ• ๋‹นํ•˜๊ณ  ์‹ถ์ง€๋งŒ, ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์œผ๋กœ ๋งค๋ฒˆ ๊ฐ’์„ ์ถ”์ถœํ•˜๊ธฐ ๊ท€์ฐฎ๊ฑฐ๋‚˜ ๋กœ์ง์ƒ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ํ™•์‹ ์ด ๋“ค ๋•Œ nil์„ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์˜ต์…”๋„์ด ์•„๋‹Œ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„์„ ์‚ฌ์šฉํ•œ๋‹ค!

- ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„๋กœ ์ง€์ •๋œ ํƒ€์ž…์€ ์ผ๋ฐ˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋‚˜, ์—ฌ์ „ํžˆ ์˜ต์…”๋„์ด๊ธฐ ๋•Œ๋ฌธ์— nil๋„ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ nil์ด ํ• ๋‹น๋˜์–ด ์žˆ์„ ๋•Œ ์ ‘๊ทผ์„ ์‹œ๋„ํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„ ๋ณ€์ˆ˜์— nil์ด ํ• ๋‹น๋˜์–ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ์ƒํ™ฉ์„ ์—ฐ์ถœํ•œ ์ฝ”๋“œ

var myName : String! = "yejin"
print(myName)   // yejin
myName = nil

// ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„๋„ ์˜ต์…”๋„์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
if let name = myName {
    print("MyName is \(name)")
} else {
    print("MyName == nil")
}
// MyName == nil

myName.isEmpty  // ์˜ค๋ฅ˜!

 

 

๐ŸŽ€ ์ •๋ฆฌ

์˜ต์…”๋„์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ•์ œ ์ถ”์ถœ ๋˜๋Š” ์•”์‹œ์  ์ถ”์ถœ ์˜ต์…”๋„์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ, nil ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ๋น„๋กฏํ•ด ๋‚˜์ค‘์— ๋ฐฐ์šธ ์˜ต์…”๋„ ์ฒด์ด๋‹ ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด ํ›จ์”ฌ ์•ˆ์ „ํ•˜๋‹ค

๋˜ํ•œ, ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ํŽธ์ด ์Šค์œ„ํ”„ํŠธ์˜ ์ง€ํ–ฅ์ ์— ๋ถ€ํ•ฉํ•˜๋‹ค!

 

 


Reference

Swift ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์•ผ๊ณฐ