Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] Class vs Struct ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] Class vs Struct

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2021. 3. 16. 22:43

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋งค์šฐ ๊ธฐ๋ณธ์ด๋ฉด์„œ๋„, ๋งค์šฐ ์ค‘์š”ํ•œ Class์™€ Struct์— ๋Œ€ํ•ด์„œ ์ข€ ์‚ดํŽด๋ณด๋ ค๊ณ  ํ•ด์š”!

๋Œ์•„๋ณด๋‹ค ๋ณด๋‹ˆ, ์ด๋Ÿฐ Swift์— ๋Œ€ํ•ด ๊ธฐ๋ณธ์ด์ง€๋งŒ ๋งค์šฐ ์ค‘์š”ํ•œ ๋‚ด์šฉ๋“ค์„ ๋”ฐ๋กœ ๋‹ค๋ฃจ์ง€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™์•„์„œ ์ข€ ๋‹ค๋ฃจ๋ ค๊ณ ์š”!! ๐Ÿง‘‍๐Ÿ’ป


๋จผ์ € Struct์™€ Class๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“  "๋ฐ์ดํ„ฐ ํƒ€์ž…"์ด์—์š”.

๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์šฉ๋„์— ๋งž๊ฒŒ ๋ฌถ์–ด ํ‘œํ˜„ํ•˜๊ณ ์ž ํ• ๋•Œ, ๋‚ด๋ถ€์—์„œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•ด์„œ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋  ์ˆ˜ ์žˆ์–ด์š”.

๋จผ์ € ๊ฐ„๋žตํ•˜๊ฒŒ ๊ณตํ†ต์ ๊ณผ ์ฐจ์ด์ ์„ ์š”์•ฝํ•ด๋ณด๊ณ , ์ค‘์š”ํ•œ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ๋Š” ์ข€ ๋” ์‚ดํŽด๋ณผ๊ฒŒ์š”!

 

 

๊ณตํ†ต์ 

  • ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœํผํ‹ฐ ์ •์˜ ๊ฐ€๋Šฅ
  • ๊ธฐ๋Šฅ ์‹คํ–‰์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ ์ •์˜ ๊ฐ€๋Šฅ
  • initializer(์ƒ์„ฑ์ž) ์ •์˜ ๊ฐ€๋Šฅ
  • extension ์„ ํ†ตํ•œ ํ™•์žฅ ๊ฐ€๋Šฅ
  • protocol ์ฑ„ํƒ ๊ฐ€๋Šฅ

 

์ฐจ์ด์ 

struct

  • Value Type
  • stack ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ €์žฅ
  • ์ฐธ์กฐ์— ๋Œ€ํ•ด ์ถ”์ ์ด ๋ถˆํ•„์š”ํ•ด์„œ ์†๋„๊ฐ€ ๋น ๋ฆ„
  • ์ƒ์† ๋ถˆ๊ฐ€๋Šฅ
  • AnyObject๋กœ ํƒ€์ž…์บ์ŠคํŒ…์ด ๋ถˆ๊ฐ€๋Šฅ
  • ์ƒ์„ฑ์ž๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์„ ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅ

class

  • Reference Type
  • heap ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ €์žฅ
  • ์ฐธ์กฐ์— ๋Œ€ํ•œ ์ถ”์ ์ด ํ•„์š”ํ•ด์„œ ์†๋„๊ฐ€ ๋Š๋ฆผ
  • ์ƒ์† ๊ฐ€๋Šฅ
  • Reference Count๊ฐ€ ์ ์šฉ (ARC ์•Œ์•„๋ณด๊ธฐ)
  • deinitializer(์†Œ๋ฉธ์ž) ์ •์˜ ๊ฐ€๋Šฅ

 

์ด ์ค‘์—์„œ, ๊ฐ€์žฅ! ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ value type์ธ์ง€, reference type์ธ์ง€ ์—์š”.

์ด ๋‘๊ฐ€์ง€์˜ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์ ์€ ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค๋กœ ๊ฐ’์„ ํ• ๋‹นํ• ๋•Œ, ๊ฐ’์ด ๋ณต์‚ฌ๋˜๋Š”์ง€, ์•„๋‹ˆ๋ฉด ๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜๋Š”์ง€์—์š”.

์ •๋ง ๊ฐ„๋‹จํ•˜์ฃ ? ๊ทธ๋Ÿฐ๋ฐ๋„ ์ค‘์š”ํ•˜๊ฒŒ ๋‹ค๋ค„์ง€๋Š” ์ด์œ ๋Š”, ์ด ๊ทผ๋ณธ์ ์ธ ์ด์œ ๋กœ ์ธํ•ด ์•ฑ์˜ ์„ฑ๋Šฅ์ด ์ขŒ์šฐ๋˜๊ธฐ๋„ ํ•˜๊ณ , Crash๋กœ ๋ถ€ํ„ฐ ์œ„ํ—˜ํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ์•ˆ์ „ํ•˜๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

 

์˜ˆ์‹œ๋ฅผ ํ•œ๋ฒˆ ๋“ค๋ฉด์„œ ์‚ดํŽด๋ณผ๊ฒŒ์š”.

struct Cat {
    var name: String
    var age: Int
}

class Dog {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

๋จผ์ € ๊ณ ์–‘์ด๋Š” struct๋กœ, ๊ฐ•์•„์ง€๋Š” class๋กœ ๋งŒ๋“ค์–ด ์คฌ์–ด์š”.

Dog๋Š” ๋ช…์‹œ์ ์œผ๋กœ init์„ ์จ์ค€ ๊ฒƒ ๋ณด์ด์‹œ๋‚˜์š”~? ์œ„์—์„œ ๋ดค๋“ฏ์ด struct๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

 

๊ทธ๋Ÿผ ๊ฐ๊ฐ ์ฐจ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”.

var cat1 = Cat(name: "cat1", age: 3)
var cat2 = cat1

cat2.name = "cat2"
cat2.age = 5

print("cat1 -> name : \(cat1.name), age: \(cat1.age)") // cat1 -> name : cat1, age: 3
print("cat2 -> name : \(cat2.name), age: \(cat2.age)") // cat2 -> name : cat2, age: 5

cat1์„ Cat์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , cat2์— ํ• ๋‹นํ•ด์คฌ์–ด์š”. ๊ทธ๋ฆฌ๊ณ  cat2์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ณ€๊ฒฝํ•ด์คฌ์–ด์š”.

๊ทธ๋ฆฌ๊ณ  cat1, cat2์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋‹ˆ.. ๋‘๊ฐœ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด์ฃ !?

์ด๊ฒŒ ์ •๋ง ์‚ฌ์†Œํ•˜์ง€๋งŒ, ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด์—์š”!!

var cat2 = cat1

์ด๋ ‡๊ฒŒ cat2์— ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ณผ์ •์—์„œ, valueํƒ€์ž…์€ ๊ฐ’๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜์š”. struct๋„ valueํƒ€์ž…์ด๋‹ˆ๊นŒ, ์ด์ˆœ๊ฐ„์— ๊ฐ’๋ณต์‚ฌ๊ฐ€ ๋ฐœ์ƒํ•ด์š”. ์ฆ‰, cat2๋Š” cat1๊ณผ ๋˜‘๊ฐ™์€ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค์ธ๊ฑฐ์ฃ !

๊ทธ๋ž˜์„œ cat2์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ฐ”๊ฟ”์คฌ์„๋•Œ, cat1๊ณผ๋Š” ์ƒ๊ด€ ์—†์ด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค์ธ cat2์˜ ์ •๋ณด๋งŒ ๋ฐ”๋€๊ฑฐ๋ž๋‹ˆ๋‹ค!

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์šฐ๋ฆฌ๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” Array, Set, Dictionary ๋“ฑ์˜ ์—ด๊ฑฐํ˜• ํƒ€์ž…์ด๋‚˜, Int, Double, String, Bool ๋“ฑ ๋ฐ์ดํ„ฐํƒ€์ž…์€ ๋ชจ๋‘ Value Type์ด์—์š”.

 

๋ฐ˜๋ฉด์—.. classํƒ€์ž…์ธ ๊ฐ•์•„์ง€์— ๋Œ€ํ•ด์„œ ๋˜‘~๊ฐ™์€ ๊ฑธ ํ•ด๋ณผ๊ฒŒ์š”.

var dog1 = Dog(name: "dog1", age: 3)
var dog2 = dog1

dog2.name = "dog2"
dog2.age = 5

print("dog1 -> name : \(dog1.name), age: \(dog1.age)") // dog1 -> name : dog2, age: 5
print("dog2 -> name : \(dog2.name), age: \(dog2.age)") // dog2 -> name : dog2, age: 5

์–ด๋ผ~~? ์ด๋ฒˆ์—๋Š” dog2๋ฅผ ๋ฐ”๊ฟ”์คฌ๋”๋‹ˆ dog1๋„ ๊ฐ™์ด ๋ฐ”๋€Œ์–ด ๋ฒ„๋ ธ์ฃ !?

์ด๊ฒŒ ๋ฐ”๋กœ reference ํƒ€์ž…์ด ํ• ๋‹น๋ ๋•Œ๋Š” ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ๊ธฐ์กด ์ธ์Šคํ„ด์Šค์ธ dog1๋ฅผ "์ฐธ์กฐ"ํ•˜๊ฒŒ ๋˜์š”. ์ฆ‰ dog1์ด๋‚˜ dog2๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ์–ด๋–ค ๋™์ผํ•œ ๊ณณ์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด์ฃ !

๋˜ ๋‹ค๋ฅด๊ฒŒ ์ด์•ผ๊ธฐ ํ•˜๋ฉด, ์ฐธ์กฐํ•˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค ๋ผ๊ณ ๋„ ์ด์•ผ๊ธฐ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ ~~?!

์ด๊ฒŒ ๋ฐ”๋กœ! ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ task๊ฐ€ ์‹คํ–‰๋  ๊ฒฝ์šฐ, ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณต์œ ๋˜๋Š” ์ž์›์œผ๋กœ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ thred-unsafetyํ•˜๋‹ค ๋ผ๊ณ  ๋งํ•˜๊ฒŒ ๋˜๋Š” ์ด์œ ์—์š”. ์—ฌ๊ธฐ ์ €๊ธฐ์„œ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐธ์กฐํ•˜๊ณ , ์ ‘๊ทผํ•˜๊ฒŒ ๋˜์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ผฌ์ด๊ฒŒ ๋˜๋Š”๊ฑฐ์ฃ ! ใ…Žใ…Ž

 

๊ฐ์ด ์ข€ ์˜ค์‹œ๋‚˜์š”~~?ใ…Žใ…Ž 

์˜ค์ผ€์ด.. ๊ทธ๋Ÿผ ์ด์ œ struct์™€ class์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์•ฝ๊ฐ„ ๊ฐ์ด ์™”์–ด.. ๊ทธ๋ž˜์„œ ์–ธ์ œ ๋ญ˜ ์จ์•ผํ•˜๋Š”๋ฐ~~?

์• ํ”Œ์€ ์นœ์ ˆํ•˜๊ฒŒ๋„ ๊ฐ€์ด๋“œ๋ผ์ธ์—์„œ, ์•„๋ž˜ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋œ๋‹ค๋ฉด "๊ตฌ์กฐ์ฒด"๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์–ด์š”!

 

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

 

์ด๋ฒˆ์—๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์™€ ์š”์•ฝ์„ ํ†ตํ•ด์„œ struct์™€ class์ฐจ์ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”~~!!

์˜ค๋Š˜๋„ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๊ธธ ๋ฐ”๋ž˜์š” ๐Ÿ™

 

 

Comments