ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] ๊ฐ’ํƒ€์ž…๊ณผ ์ฐธ์กฐํƒ€์ž… ( Value Type vs Reference Type )
    iOS/๐ŸŸ  Swift 2020. 12. 29. 18:30

    1์ฐจ ์ˆ˜์ •: 2021.06.06

     

     

    ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์Šค์œ„ํ”„ํŠธ์˜ ๊ฐ’ํƒ€์ž…๊ณผ ์ฐธ์กฐํƒ€์ž…์˜ ์ฐจ์ด์ ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š” ๊ด€์ ์—์„œ ๋‹ค๋ฃจ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿถ

     

    *์†์„ฑ๊ฐ์‹œ์ž์—์„œ ๊ฐ’ํƒ€์ž…๊ณผ ์ฐธ์กฐํƒ€์ž…์˜ ์ž‘๋™์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด? -> ์†์„ฑ๊ฐ์‹œ์ž๋ž€ (Property Observer) ?


     

    Swift์—์„œ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

    ๊ฐ’ ํƒ€์ž… ์ฐธ์กฐ ํƒ€์ž…
    Structure
    Enumeration
    Tuple
    Class
    Closure
    /* Value Type */
    struct testStruct {
      var name = ""
      var age = 0
    }
    
    /* Reference Type */
    class testClass {
      var name = ""
      var age = 0
    }

     

    ๋Œ€ํ‘œ์ ์ธ ํƒ€์ž…์ธ Struct์™€ Class๋ฅผ ํ†ตํ•ด ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ๊ฐ๊ฐ์€ ๊ฐ™์€ ์†์„ฑ์„ ๊ฐ€์ง€์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

     

    ํ•œ ์žฅ์˜ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


     

    ๊ฐ’ ํƒ€์ž…

    // Value Type
    var testStruct = testStruct()
    
    /* ๋ณ„๊ฐœ์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๊ฐ’์ด ๋ณต์‚ฌ๋จ */
    var anotherStruct = value1
    
    anotherStruct.name = "John Doe"
    anotherStruct.age = 10 // testStruct.name: "", testStruct.age: 0 

    ๊ฐ’ ํƒ€์ž…์ธ struct ์˜ ๊ฒฝ์šฐ, stack ์˜์—ญ์— ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

    ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ์ „๋‹ฌ ์‹œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์‚ฌ๋ฉ๋‹ˆ๋‹ค.


     

    ์ฐธ์กฐ ํƒ€์ž…

    // Reference Type
    var testClass = testClass()
    
    /* ์ฃผ์†Œ ๊ฐ’๋งŒ ๋ณต์‚ฌ๋˜๋ฏ€๋กœ, ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด */
    var anotherClass = ref1
    
    anotherClass.name = "Jane Doe"
    anotherClass.age = 17 // testClass.name: "Jane Doe", testClass.age: 17 

     

    ์ฐธ์กฐ ํƒ€์ž…์ธ class์˜ ๊ฒฝ์šฐ, heap ์˜์—ญ์— ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๊ณ , stack ์˜์—ญ์—๋Š” heap ์˜์—ญ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์˜ ์ „๋‹ฌ์€ ์ธ์Šคํ„ด์Šค๊ฐ€ ์œ„์น˜ํ•œ ์‹ค์ œ ์ฃผ์†Œ(= heap ์˜์—ญ์˜ ์ฃผ์†Œ)์˜ ๋ณต์‚ฌ์ด๋ฉฐ,

    ๊ฒฐ๊ณผ์ ์œผ๋กœ ์œ„ ์ฝ”๋“œ์—์„œ  ref2๋Š” ref1์˜ ์ฐธ์กฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ( Retain Count๋Š” 1 ์ฆ๊ฐ€ )


     

    let ํ‚ค์›Œ๋“œ

    let์œผ๋กœ ์„ ์–ธ๋œ ์ธ์Šคํ„ด์Šค๋Š”, ์ธ์Šคํ„ด์Šค์— ํ• ๋‹น๋œ stack ์˜์—ญ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ stack ์˜์—ญ์— ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฐ’ ํƒ€์ž… ์ธ์Šคํ„ด์Šค ๋“ค์€ ์†์„ฑ์„ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

     

    ํ•˜์ง€๋งŒ stack ์˜์—ญ์— heap์˜์—ญ์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋˜๋Š” ์ฐธ์กฐ ํƒ€์ž… ์ธ์Šคํ„ด์Šค๋“ค์€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ๋งŒ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ•  ๋ฟ, ์†์„ฑ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ์ด๊ฒƒ์ด ๋ฉ”๋ชจ๋ฆฌ ๊ด€์ ์—์„œ์˜ ๊ฐ’ ํƒ€์ž…๊ณผ ์ฐธ์กฐ ํƒ€์ž…์˜ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค.

     

     

    ๋!


    ๐Ÿค–๐ŸŸข[์šฐ์งฑ์˜ iOS ๋ธ”๋กœ๊ทธ]๐Ÿ”ต๐Ÿ’ป

    iOS๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ธฐ๋กํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋‹ค์Œ ๊ธ€: ARC - Memory

     

    ARC - Memory

    "Auto Reference Counting" ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ๋‚จ์•„์žˆ๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋Š” ์•ฑ ์„ฑ๋Šฅ์˜ ์ €ํ•˜๋กœ ์ด์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค์˜ ์ ์ ˆํ•œ ํ•ด์ œ๋Š” ์ค‘์š”ํ•œ ์ด

    woozzang.tistory.com

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

    Strong Reference - Memory  (0) 2020.12.31
    ARC - Memory  (0) 2020.12.30
    Mutability of Collections - Collections  (0) 2020.12.01
    Self Type - General  (0) 2020.11.27
    [Swift] Property Observer (์†์„ฑ ๊ฐ์‹œ์ž) ๋ž€?  (0) 2020.11.27

    ๋Œ“๊ธ€

์–ด์ œ๋ณด๋‹ค ๋ฐœ์ „ํ•œ ๋‚˜