Deep_Dev
article thumbnail

 

์˜ต์…”๋„ ์ฒด์ด๋‹

์˜ต์…”๋„์— ์†ํ•ด ์žˆ๋Š” nil ์ผ์ง€๋„ ๋ชจ๋ฅด๋Š” ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ, ์„œ๋ธŒ์Šคํฌ๋ฆฝ์…˜ ๋“ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ จ์˜ ๊ณผ์ •. ์ฆ‰, ์˜ต์…”๋„์„ ๋ฐ˜๋ณต์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…”๋„์ด ์ž์ „๊ฑฐ ์ฒด์ธ์ฒ˜๋Ÿผ ์„œ๋กœ ๊ผฌ๋ฆฌ๋ฅผ ๋ฌผ๊ณ  ์žˆ๋Š” ๋ชจ์–‘์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ์ฒด์ด๋‹์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์ž์ „๊ฑฐ ์ฒด์ธ์—์„œ ํ•œ ์นธ์ด๋ผ๋„ ์—†๊ฑฐ๋‚˜ ๊ณ ์žฅ ๋‚˜๋ฉด ์ฒด์ธ ์ „์ฒด๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋“ฏ์ด ์ค‘์ฒฉ๋œ ์˜ต์…”๋„ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ nil ๋ฐ˜ํ™˜

 

์˜ต์…”๋„ ์ฒด์ด๋‹์€ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ ๋˜๋Š” ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ์€ ์˜ต์…”๋„ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ๋’ค์— ๋ฌผ์Œํ‘œ(?)๋ฅผ ๋ถ™์—ฌ ํ‘œํ˜„ํ•œ๋‹ค.

์˜ต์…”๋„์ด nil์ด ์•„๋‹ˆ๋ผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ํ˜ธ์ถœ๋  ๊ฒƒ์ด๊ณ , nil์ด๋ผ๋ฉด ๊ฒฐ๊ด๊ฐ’์œผ๋กœ nil์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค.

 


! ( ๋Š๋‚Œํ‘œ )

๋ฌผ์Œํ‘œ ๋Œ€์‹ ์— ๋Š๋‚Œํ‘œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„์žˆ๋Š”๋ฐ, ์ด๋Š” ์˜ต์…”๋„์—์„œ ๊ฐ’์„ ๊ฐ•์ œ ์ถ”์ถœํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. ๋ฌผ์Œํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๊ฐ’์„ ๊ฐ•์ œ์ถ”์ถœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„์— ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์ ์€ ์˜ต์…”๋„์—์„œ ๊ฐ’์„ ๊ฐ•์ œ ์ถ”์ถœํ•ด ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜ ๊ฐ’์ด ์˜ต์…”๋„์ด ์•„๋‹ˆ๋ผ๋Š” ์ ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ •๋ง 100% nil์ด ์•„๋‹ˆ๋ผ๋Š” ํ™•์‹ ์„ ํ•˜๋”๋ผ๋„ ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.

 

์˜ต์…”๋„ ์ฒด์ด๋‹์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•œ ์‚ฌ๋žŒ์˜ ์ฃผ์†Œ ์ •๋ณด ํ‘œํ˜„ ์„ค๊ณ„

Class Room{ // ํ˜ธ์‹ค
	var number: Int // ํ˜ธ์‹ค ๋ฒˆํ˜ธ
    
    init(number: Int) {
    	self.number = number
    }
}

class Building { // ๊ฑด๋ฌผ
	var name: String // ๊ฑด๋ฌผ ์ด๋ฆ„
    var room: Room? // ํ˜ธ์‹ค ์ •๋ณด
    
    init(name: String){
    	self.name = name
    }
}

struct Address { // ์ฃผ์†Œ
	var province: String // ๊ด‘์—ญ์‹œ/๋„
    var city: String // ์‹œ/๊ตฐ/๊ตฌ
    var street: String // ๋„๋กœ๋ช…
    var building: Building? // ๊ฑด๋ฌผ
    var detailAddress: String? // ๊ฑด๋ฌผ ์™ธ ์ƒ์„ธ์ฃผ์†Œ
}

class Person{ // ์‚ฌ๋žŒ
	var name: String // ์ด๋ฆ„
    var address: Address? // ์ฃผ์†Œ
    
    init(name: String){
    	self.name = name
    }
}

( Person ํด๋ž˜์Šค๋Š” ์ด๋ฆ„์ด ์žˆ๊ณ  ์ฃผ์†Œ๋Š” ์˜ต์…”๋„๋กœ ๊ฐ–๋Š”๋‹ค )

// yagom ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
let yagom : Person = Person(name: "yagom")

---> yagom์ด ์‚ฌ๋Š” ํ˜ธ์‹ค ๋ฒˆํ˜ธ๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค. 

let yagomRoomViaOptionalChaining: Int? = yagom.address?.building.?.room?.number // nil
let yagomRoomViaOptionalChaining: Int? = yagom.address!.building.!.room!.number // ์˜ค๋ฅ˜

yagom ์—๋Š” ์•„์ง ์ฃผ์†Œ, ๊ฑด๋ฌผ, ํ˜ธ์‹ค ์ •๋ณด๊ฐ€ ์—†๋‹ค. ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ์‚ฌ์šฉํ•˜๋ฉด yaom์˜ address ํ”„๋กœํผํ‹ฐ๊ฐ€ nil์ด๋ฏ€๋กœ ์˜ต์…”๋„ ์ฒด์ด๋‹ ๋„์ค‘ nil์ด ๋ฐ˜ํ™˜๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ! ๋กœ ๊ฐ•์ œ ์ถ”์ถœ์„ ์‹œ๋„ํ–ˆ์„๋• nil์ธ address ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋ ค ํ•  ๋•Œ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

 

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ๊ณผ ์˜ต์…”๋„ ์ฒด์ด๋‹ ๋น„๊ต

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ ์‚ฌ์šฉ

let yagom: Person = Person(name: "yagom")
var roomNumber: Int? = nil
if let yagomAddress: Address = yagom.address{
	if let yagomBuilding: Building = yagomAddress.building {
    	if let yagomRoom: Room = yagomBuilding.room {
        	roomNumber = yagomRoom.number
        }
    }
}

if let number: Int = roomNumber{
	print(number)
} else {
	print("Can not find room number")
}

 

์˜ต์…”๋„ ์ฒด์ด๋‹์˜ ์‚ฌ์šฉ

let yagom: Person = Person(name: "yagom")

if let roomNumber: Int = yagom.address?.building?.room?.number {
	print(roomNumber)
} else {
	print("Can not find room number")
}

์˜ต์…”๋„ ์ฒด์ด๋‹์˜ ๊ฒฐ๊ด๊ฐ’์€ ์˜ต์…”๋„ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ๊ณผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋น ๋ฅธ ์ข…๋ฃŒ Guard

  • if ๊ตฌ๋ฌธ : '~๋ฉด ํ•ด๋ผ'
  • guard ๊ตฌ๋ฌธ : '~ ์•„๋‹ˆ๋ฉด ๋๋‚ด๋ผ' --> ๋น ๋ฅธ ์ข…๋ฃŒ ! 
  • ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ์ข…๋ฃŒ์‹œ์ผœ์•ผ ํ•˜๋„๋ก ๋ฌธ๋ฒ• ์ž‘์„ฑ ( return / throw )

if ๊ตฌ๋ฌธ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ Bool ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

guard๋’ค์— ๋”ฐ๋ผ๋ถ™๋Š” ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ true์ผ๋•Œ ์ฝ”๋“œ๊ฐ€ ๊ณ„์† ์‹คํ–‰๋œ๋‹ค. 

if ๊ตฌ๋ฌธ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ guard ๊ตฌ๋ฌธ์€ ํ•ญ์ƒ else ๊ตฌ๋ฌธ์ด ๋’ค์— ๋”ฐ๋ผ์™€์•ผ ํ•œ๋‹ค.

 

 

๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ


guard Boolํƒ€์ž… ๊ฐ’ else {

          ์˜ˆ์™ธ์‚ฌํ•ญ ์‹คํ–‰๋ฌธ
          ์ œ์–ด๋ฌธ ์ „ํ™˜ ๋ช…๋ น์–ด
}

 

๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋Š” if ๊ตฌ๋ฌธ๊ณผ guard ๊ตฌ๋ฌธ

// if ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ
for i in 0...3 {
	if i == 2 {
    	print(i)
    } else {
    	continue
    }
}

// guard ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ
for i in 0...3 {
	guard i == 2 else {
    	continue
    }
    print(i)
}

 

์‚ฌ์šฉ ์ด์œ  : ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ•œ๊ณ„์ 

  • ํ•จ์ˆ˜ / ๋ฉ”์„œ๋“œ / ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ž์‹ ์„ ๊ฐ์‹ธ๋Š” ์ฝ”๋“œ ๋ธ”๋ก. ์ฆ‰, return, break, continue, throw ๋“ฑ์˜ ์ œ์–ด๋ฌธ ์ „ํ™˜ ๋ช…๋ น์–ด๋ฅผ ์“ธ ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅ