Swift

[Swift] ํƒ€์ž…์บ์ŠคํŒ… : is, as

yevdev 2022. 8. 13. 14:24

๐Ÿ’ก ํƒ€์ž…์บ์ŠคํŒ…?

- ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ™์€ ๊ณ„์ธต(hierachy)์— ์žˆ๋Š” ๋‹ค๋ฅธ superclass๋‚˜ subclass๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

 

is

  • Checking Type
  • ๋ง ๊ทธ๋Œ€๋กœ, ํŠน์ • ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค์›Œ๋“œ
  • ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ์‹ค์ œ ์ฒดํฌ๊ฐ€ ์ด๋ค„์ง
  • ๋ฐ˜ํ™˜ ํ˜•์€ Bool
let exChar: Character = "A"

print(exChar is Character)	// true
print(exChar is Int)	// false

→ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ 

import Foundation

class Human { }
class Student: Human { }

let yejin: Student = .init()
print(yejin is Student)	// true

let mingyu: Human = .init()
print(mingyu is Student)	// false

→ ์ด๋ ‡๊ฒŒ ์ƒ์†๋˜์–ด ์žˆ๋Š” class ๋ฅผ ํ†ตํ•ด, ํ‘œํ˜„์‹์ด ํƒ€์ž…์˜ ์„œ๋ธŒํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ์—๋„ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

→ yejin์€ Human ํด๋ž˜์Šค์˜ ์ž์‹ ํด๋ž˜์Šค์ธ Student์— ํ•ด๋‹น๋˜์–ด, ํƒ€์ž…์ฒดํฌ๋ฅผ ํ•ด๋ณด๋ฉด true

→ mingyu๋Š” Human ํด๋ž˜์Šค์ผ ๋ฟ์ด์ง€ ์ž์‹ํด๋ž˜์Šค์ธ Student์— ํ•ด๋‹น๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ํƒ€์ž…์ฒดํฌ๋ฅผ ํ•ด๋ณด๋ฉด false

 

 

 

as

  • Up Casting
  • ์„œ๋ธŒ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ "์Šˆํผ ํด๋ž˜์Šค์˜ ํƒ€์ž…"์œผ๋กœ ์ฐธ์กฐ
  • ์—… ์บ์ŠคํŒ…์€ ํ•ญ์ƒ ์„ฑ๊ณตํ•จ
  • ์ปดํŒŒ์ผ ์‹œ์ ์— ์บ์ŠคํŒ… ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •
import Foundation

class Human {
    let name: String = "yejin"
}
class Student: Human {
    let major: String = "CE"
}

let yejin = Student.init() as Human

print(yejin.name)	// yejin
print(yejin.major)	// ๐Ÿšซ Value of type 'Human' has no member 'major'

์—ฌ๊ธฐ์„œ 

let yejin = Student.init() as Human

์€, "Student ํƒ€์ž…์œผ๋กœ yejin ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์ง€๋งŒ, Human ํƒ€์ž…์œผ๋กœ ์—…์บ์ŠคํŒ…ํ•ด์„œ yejin์— ์ €์žฅ" ์ด๋ผ๋Š” ์˜๋ฏธ!

→ ์‹ค์ œ, yejin ํƒ€์ž…์€ ์—…์บ์ŠคํŒ…๋œ Human ํƒ€์ž…

 yejin์˜ ์ ‘๊ทผ๋ฒ”์œ„๋Š” Human์œผ๋กœ ํ•œ์ •๋จ.

 

๋”ฐ๋ผ์„œ, 

print(yejin.name)	// yejin
print(yejin.major)	// ๐Ÿšซ Value of type 'Human' has no member 'major'

์ด๋ ‡๊ฒŒ major์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ๋จ.

 

์—…์บ์ŠคํŒ…์„ ํ•  ๋•Œ๋Š”,

let yejin = Student.init() as Human
// ์œ„์ฒ˜๋Ÿผ ํ•ด๋„ ๋˜๊ณ 
// ์•„๋ž˜์ฒ˜๋Ÿผ Type Annotation์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.
let yejin: Human = Student.init()

 

 

as? as!

  • Down Casting
  • up casting๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ์Šˆํผํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์„œ๋ธŒํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ์ฐธ์กฐํ•จ
  • ์—…์บ์ŠคํŒ…๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ค์‹œ ์›๋ž˜ ์„œ๋ธŒํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์ฐธ์กฐํ•  ๋•Œ ์‚ฌ์šฉ
  • upcasting์ฒ˜๋Ÿผ ํ•ญ์ƒ ์„ฑ๊ณตํ•  ์ˆ˜ ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ์„œ as?, as! ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์•„๊นŒ ์—…์บ์ŠคํŒ… ์˜ˆ์ œ์—์„œ 

print(yejin.major)	// ๐Ÿšซ Value of type 'Human' has no member 'major'

์š”๋กœ์ผ€ major ์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†์—ˆ๋Š”๋ฐ 

์—…์บ์ŠคํŒ…๋œ yejin ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ค์‹œ ์›๋ž˜ ์„œ๋ธŒํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์šด์บ์ŠคํŒ…์„ ํ•ด์ค€๋‹ค!

let student: Student = yejin as! Student

print(student.name)	// yejin
print(student.major)	// CE

 

๋งŒ์•ฝ as! ๋Œ€์‹  as?๋ฅผ ์“ด๋‹ค๋ฉด?

"๋Ÿฐํƒ€์ž„ ์‹œ์ "์— ํƒ€์ž… ์บ์ŠคํŒ…(๋‹ค์šด ์บ์ŠคํŒ…)์„ ํ•˜๋ฉฐ, ์‹คํŒจํ•  ๊ฒฝ์šฐ nil์„ ๋ฆฌํ„ด

 

 as?๋Š” ์‹คํŒจํ•  ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„! ์•„์ฃผ ์•ˆ์ „~!

→ nil์„ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Optional-Type์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.

 

 

'Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] lazy?  (0) 2022.08.21
[Swift] Any / AnyObject  (0) 2022.08.14
[Swift] ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ  (0) 2022.08.13
[Swift] ์ƒ์†  (0) 2022.08.02
[Swift] ๋ฐฑ์ค€ : 10814 (๊ตฌ์กฐ์ฒด)  (0) 2022.08.02