ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [iOS] NSCoding ์ด๋ž€? (feat. ์ธ์ฝ”๋”ฉ, ๋””์ฝ”๋”ฉ, ์•„์นด์ด๋น™)
    iOS/๐ŸŽ Documentation 2021. 5. 23. 20:48

    ์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ ๊ณต์‹๋ฌธ์„œ์—์„œ ์„ค๋ช…ํ•˜๋Š” NSCoding ๋ฅผ ๊ณต๋ถ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿถ


     

    NSCoding

    protocol NSCoding
    

    NSCoding์€ ํด๋ž˜์Šค๊ฐ€ encoding ๊ณผ decoding ๋˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํ”„๋กœํ† ์ฝœ์ž…๋‹ˆ๋‹ค.

     

    ๋‹ค์‹œ ๋งํ•˜๋ฉด, NSCoding ์„ ์ฑ„ํƒํ•œ ํด๋ž˜์Šค๋Š” encoding ๊ณผ decoding ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

     

    https://velog.io/@chan33344/Codable-์ธ์ฝ”๋”ฉ-๋””์ฝ”๋”ฉ์ด๋ž€

     

    ์ธ์ฝ”๋”ฉ์€ ๊ธฐ์กด์˜ ํ˜•์‹์„ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

     

    ์˜ˆ๋ฅผ ๋“ค์–ด Swift ์˜ Struct ์ธ์Šคํ„ด์Šค๋ฅผ JSON ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๊ฒƒ์€ JSON Encoding ์ด๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋””์ฝ”๋”ฉ์€ ๊ทธ ๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  Encoding ๊ณผ Decoding ์€ ์•„์นด์ด๋น™(Archiving) ๊ณผ  ๋””์ŠคํŠธ๋ฆฌ๋ทฐ์…˜(Distribution) ์— ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ฐœ์š”

    ํ”„๋กœํ† ์ฝœ์€ ์š”๊ตฌ์‚ฌํ•ญ์ด๋ฏ€๋กœ, NSCoding ํ”„๋กœํ† ์ฝœ์€ ์ด ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ ํด๋ž˜์Šค๊ฐ€ ๋‘ ๊ฐ€์ง€ ๋ฉค๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค.

     

    NSCoding ์„ ์ฑ„ํƒํ•œ UIViewController์—์„œ init?(coder:) ๋ฅผ ์ด๋ฏธ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค๐Ÿ˜Ž

     

    Encoding ๊ณผ Decoding ์€ ์•„์นด์ด๋น™(Archiving) ๊ณผ  ๋””์ŠคํŠธ๋ฆฌ๋ทฐ์…˜(Distribution)์˜ ๊ธฐ๋ณธ ์กฐ๊ฑด์ž…๋‹ˆ๋‹ค.

     

    ์—ฌ๊ธฐ์„œ ์•„์นด์ด๋น™(Archiving)์€ ์˜ค๋ธŒ์ ํŠธ๋‚˜ ์–ด๋–ค ๊ตฌ์กฐ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋””์Šคํฌ์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

     


    ๐Ÿ’ก ์ข€ ๋” ์ž์„ธํžˆ, Archives๋ž€?

    ์˜ค๋ธŒ์ ํŠธ์™€ ๊ฐ’์„ ์•„ํ‚คํ…์ฒ˜์— ์ข…์†๋˜์ง€ ์•Š๋Š” ๋ฐ”์ดํŠธ์˜ ์—ด๋“ค( stream of bytes) ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  ์•„์นด์ด๋น™์„ ํ•˜๋Š” ์ฃผ์ฒด๋Š” ์•„์นด์ด๋ฒ„(Archiver) ์ž…๋‹ˆ๋‹ค. NSKeyedArchiver  ๋“ฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

     

    ์ด ๋•Œ ์ด ๋ฐ”์ดํŠธ ์—ด์€ ์˜ค๋ธŒ์ ํŠธ์™€ ๊ฐ’๋“ค ์‚ฌ์ด์˜ ๊ด€๊ณ„์™€ identiy ๋Š” ์œ ์ง€ํ•œ ์ƒํƒœ๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    Cocoa archives can hold Objective-C objects, scalars, arrays, structures, and strings

    ์ถœ์ฒ˜ :  Archives and Serializations Programming Guide


     

    ๊ทธ๋ฆฌ๊ณ  ๋””์ŠคํŠธ๋ฆฌ๋ทฐ์…˜(Distribution)์€ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ •์˜ ๋œ ๊ณณ ๋ฐ–์˜ ๋‹ค๋ฅธ ์ฃผ์†Œ ๊ณต๊ฐ„์— ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

    ์•„์นด์ด๋น™๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๋‹ค๋ฅธ ๊ณณ์—์„œ ๋””์ฝ”๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ด๋Ÿฐ ์˜๋ฏธ๋กœ ์“ด ๊ฒƒ์œผ๋กœ ์ดํ•ดํ–ˆ๋‹ค.

     

    NSCoding ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ฒŒ ๋˜๋ฉด NSCoder ํƒ€์ž…์˜ coder ๊ฐ€ ์˜ค๋ธŒ์ ํŠธ์˜ ๋ฉ”์„œ๋“œ encode(with:) ์™€ init(coder:) ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

     

    encode(with:) ๋Š” ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ž์‹ ์—๊ฒŒ ์ œ๊ณต๋ฐ›์€ coder ๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ธŒ์ ํŠธ๋Š” ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜์ฐจ๋ก€ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    โ—๏ธ"A๋ผ๋Š” ์˜ค๋ธŒ์ ํŠธ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•œ๋‹ค" = "A์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค" ์ด๋ฏ€๋กœ, ์œ„ ๋ฌธ์žฅ์€ encode(with:) ๋ฉ”์„œ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

     

    init(coder:) ๋Š” ์ „๋‹ฌ๋ฐ›์€ coder ์— ๋“ค์–ด์žˆ๋Š” data (๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ) ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ์˜ค๋ธŒ์ ํŠธ ํ˜•์‹์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๊ฒƒ์€ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ๋Œ€์ฒดํ•˜๊ณ , ํ•˜๋‚˜์˜ ์˜ค๋ธŒ์ ํŠธ๋‹น ํ•œ๋ฒˆ๋งŒ ๋ณด๋‚ด์ง‘๋‹ˆ๋‹ค.

     

     ์–ด๋–ค ์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค๊ฐ€ codeable ( encoding ๊ณผ decoding์ด ๊ฐ€๋Šฅํ•œ) ํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ NSCoding ํ”„๋กœํ† ์ฝœ๋ฅผ ์ฑ„ํƒํ•˜์—ฌ ๊ทธ ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ตฌํ˜„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. (์—ฌ๊ธฐ์„œ Codable ํ”„๋กœํ† ์ฝœ์€ ์•„์ง ์ƒ๊ฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ^^)

     

    macOS 10.2 ์ดํ›„ ๋ถ€ํ„ฐ๋Š” KeyedArchiving ์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

    Keyed archives ๋Š” NSKeyedArchiver ์™€ NSKeyedUnarchiver ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.


     

    ์ •๋ฆฌ

    ๊ฐ์ฒด๋ฅผ ์•„์นด์ด๋น™ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด (ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด) NSObjcet ๋ฅผ ์ƒ์†ํ•˜๊ณ , NSCoding ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    ํ•˜์ง€๋งŒ ๊ตฌ์กฐ์ฒด๋Š” ์•„์นด์ด๋น™ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ตฌ์กฐ์ฒด์˜ ๊ฒฝ์šฐ Encodable ๊ณผ Decodable ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

     

     


     

    ์•„์ง ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ ๋ถ€๋ถ„ ๐Ÿค”

    archivedData(withRootObject:requiringSecureCoding:)

    NSKeyedArchiver ์˜ ํƒ€์ž… ๋ฉ”์„œ๋“œ์ธ๋ฐ, 'root of the object graph to archive' ๋ผ๋Š” ํ‘œํ˜„์ด ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

     

    ์•„์นด์ด๋น™ ๋Œ€์ƒ์˜ ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ค ํด๋ž˜์Šค์˜ ์„œ๋ธŒ ํด๋ž˜์Šค์ด๋ฉด, ๊ทธ ํด๋ž˜์Šค ๊ณ„์ธต์˜ ๋ฃจํŠธ ํด๋ž˜์Šค๋ฅผ ์ „๋‹ฌํ•˜๋ผ๋Š” ์˜๋ฏธ ์ธ๊ฑด๊ฐ€?

     

     

     

    ๋!


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

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

     

     

    ์ฐธ๊ณ  ์ž๋ฃŒ 1: https://developer.apple.com/documentation/foundation/nscoding

    ๋Œ“๊ธ€

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