๐ŸŽ ์•„์ด-์˜ค-์—์Šค/๐Ÿ“ฑ iOS ๊ฐœ๋ฐœ

[iOS] required init?(coder: NSCoder)์˜ ์ •์ฒด๋ฅผ ์ฐพ์•„์„œ

๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด 2022. 1. 11. 23:54

NSCoder

UIView๋ฅผ ์ƒ์†ํ•ด ์ปค์Šคํ…€ํ•˜๊ฒŒ ๋ทฐ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋‘ ์ข…๋ฅ˜์˜ ์ƒ์„ฑ์ž๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์„ฑ์ž๋Š” frame์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž์˜€์ฃ . ๊ทธ๋Ÿผ NSCoder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์„ฑ์ž๋Š” ์–ธ์ œ ์“ฐ์ผ๊นŒ์š”?

 

์ด ๋ฌผ์Œ์— ๋‹ต์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” NSCoder๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ถ€ํ„ฐ ์•Œ์•„์•ผ๊ฒ ์ฃ ? NSCoder์˜ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๊ฐ€๋ด…์‹œ๋‹ค.

์ผ๋‹จ ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” NSCoder๋ฅผ ์ถ”์ƒํด๋ž˜์Šค๋ผ๊ณ  ์†Œ๊ฐœํ•˜๊ณ  ์žˆ์–ด์š”. ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์•„์นด์ด๋น™(archiving. ์••์ถ•์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?) ํ•˜๊ฑฐ๋‚˜ ๋ถ„๋ฐฐ(distribution)ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ์˜ค๋ฒ„๋ทฐ์—์„œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ฝ์–ด๋ณผ๊ฒŒ์š”.

 

 NSCoder declares the interface used by concrete subclasses to transfer objects and other values between memory and some other format. This capability provides the basis for archiving (storing objects and data on disk) and distribution (copying objects and data items between different processes or threads).

 

์—ฌ๊ธฐ์„œ ์„ค๋ช…์ด ๋ช…ํ™•ํ•ด์ง€๋„ค์š”. NSCoder๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ  ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์„œ๋ธŒํด๋ž˜์Šค๋Š” ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์™€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํฌ๋งท ์‚ฌ์ด์—์„œ ์ „ํ™˜์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๊ฑด ์•„์นด์ด๋น™๊ณผ ๋””์ŠคํŠธ๋ฆฌ๋ทฐ์…˜์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜์ฃ .

 

Archiving์€ ๊ฐ์ฒด์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋””์Šคํฌ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ณ  Distribution์€ ๊ฐ์ฒด์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋‚˜ ์Šค๋ ˆ๋“œ์‚ฌ์ด์—์„œ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜๋„ค์š”.

 

The concrete subclasses provided by Foundation for these purposes are NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder. Concrete subclasses of NSCoder are “coder classes”, and instances of these classes are “coder objects” (or simply “coders”). A coder that can only encode values is an “encoder”, and one that can only decode values is a “decoder”.

 

NSCoder๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์€ Foundation ํ”„๋ ˆ์ž„์›Œํฌ์—๋„ ํฌํ•จ๋˜์–ด ์žˆ๋Š”๋ฐ์š”, NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, NSPortCoder๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํด๋ž˜์Šค๋“ค์„ "Coder Classes"๋ผ๊ณ  ํ•˜๊ณ , ์ด ํด๋ž˜์Šค๋“ค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ "Codier Objects(ํ˜น์€ Coders)"๋ผ๊ณ  ํ•ด์š”.

 

๊ทธ๋ฆฌ๊ณ  ์ธ์ฝ”๋”ฉ์„ ์ง€์›ํ•˜๋Š” Coder๋ฅผ Encoder, ๋””์ฝ”๋”ฉ์„ ์ง€์›ํ•˜๋Š” Coder๋ฅผ Decoder๋ผ๊ณ  ํ•˜์ฃ . ์ด๊ฑด ๋‹ค ์•„๋Š” ์ด์•ผ๊ธฐ์ฃ ?

 A coder stores object type information along with the data, so an object decoded from a stream of bytes is normally of the same class as the object that was originally encoded into the stream. An object can change its class when encoded, however; this is described in Archives and Serializations Programming Guide.

Coder๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ๋””์ฝ”๋”ฉ์ด๋  ๋•Œ ์ด ํƒ€์ž…์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์ธ์ฝ”๋”ฉํ–ˆ๋˜ ํƒ€์ž…์ด ๋””์ฝ”๋”ฉ ์‹œ์— ๊ทธ๋Œ€๋กœ ์ ์šฉ๋˜์ฃ .

 

์ด๋ ‡๊ฒŒ ๋ถˆ์นœ์ ˆํ•˜๊ฒŒ ๋ฌธ์„œ๋ฅผ ์ ์–ด๋‘๊ณ  ๊ฐ€์žฅ ๋ฐ‘์—์„œ ๋‘ ๊ฐœ์˜ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด๋ผ๊ณ  ํ•˜๊ณ  ์žˆ๋„ค์š”.. NSCoder์˜ ์„œ๋ธŒํด๋ž˜์‹ฑ์— ๋Œ€ํ•œ ๋‚ด์šฉ๊ณผ Archiving๊ณผ Serialization์— ๋Œ€ํ•œ ๊ฐ€์ด๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ €ํฌ๊ฐ€ ์ง์ ‘ NSCoder์˜ ์„œ๋ธŒํด๋ž˜์Šค๋Š” ๊ตฌํ˜„ํ•  ์ผ์ด ๋งŽ์ด ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ ์•„์นด์ด๋น™๊ณผ ์ง๋ ฌํ™”์— ๋Œ€ํ•œ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณผ๊ฒŒ์š”!

 

Archives and Serializations Programming Guide

 

Introduction

Introduction Archives and serializations are two ways in which you can create architecture-independent byte streams of hierarchical data. Byte streams can then be written to a file or transmitted to another process, perhaps over a network. When the byte st

developer.apple.com

Intro

์•„์นด์ด๋น™๊ณผ ์ง๋ ฌํ™”๋Š” ๊ณ„์ธต์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์ด ๋˜๋ฉด ํŒŒ์ผ์— ์“ฐ์—ฌ์งˆ ์ˆ˜๋„ ์žˆ๊ณ , ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „์†ก์ด ๋  ์ˆ˜๋„ ์žˆ์ฃ . ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์ด ๋””์ฝ”๋”ฉ๋˜๋ฉด ๊ธฐ์กด ๊ณ„์ธต์ด ๊ทธ๋Œ€๋กœ ์žฌ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์•„์นด์ด๋น™์€ ๊ฐ์ฒด๋“ค์ด ์„œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ด€๊ณ„๋“ค๊ณผ ๊ฐ’์„ ๊ธฐ๋กํ•ด์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ง๋ ฌํ™”๋Š” ๊ณ„์ธต๊ฐ„์˜ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’๋“ค์ด ๊ฐ€์ง€๋Š” ๊ฐ’๋“ค์„ ๋ฆฌ์ŠคํŠธ์—… ํ•ด๋‘๋Š” ๊ฒƒ ๋ฟ์ด์ฃ .

 

๋ฌด์Šจ ์†Œ๋ฆฐ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์ฃ ? ์ผ๋‹จ ์—ฌ๊ธฐ์„œ ํ•˜๊ณ ์ž ํ•˜๋Š”๊ฑด ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ”๊พธ๊ณ ์žํ•˜๋Š” ๊ฒƒ์ด๊ณ , ์ด๋•Œ ๋ฐ์ดํ„ฐ๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณ„์ธต๊ด€๊ณ„๋„ ํฌํ•จํ•˜๊ณ  ์‹ถ์€๊ฑฐ์˜ˆ์š”. ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ”๊พธ๋ ค๋Š” ์ด์œ ๋Š” ํŒŒ์ผ์— ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜, ๋„คํŠธ์›Œํฌ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๊ตฌ์š”. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ”๊พธ๋ ค๋Š” "๋ฐ์ดํ„ฐ"๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์กฐ๊ธˆ ์ถ”์ƒ์ ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ ๋ฐ”๋กœ ๋‹ค์Œ ์„น์…˜์„ ํ†ตํ•ด์„œ ํžŒํŠธ๋ฅผ ์–ป์–ด๋ณด์ž๊ตฌ์š”!

 

Object Graphs


๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“œ๋Š” ๋ชจ๋“  ์•ฑ์ด ๋‹ค ๊ทธ๋ ‡๊ฒ ์ฃ ..!)์€ ๊ฐ์ฒด๋“ค์ด ๊ต‰์žฅํžˆ ๋ณต์žกํ•˜๊ฒŒ ์–ฝํ˜€์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ธฐ๋„ ํ•˜๊ณ , ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์†Œ์œ ํ•˜๊ธฐ๋„ ํ•˜์ฃ . ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋“ค์ด ์–ฝํžˆ๊ณ  ์–ฝํ˜€์žˆ๋Š” ๊ตฌ์กฐ(Web์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋„ค์š”)๋ฅผ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„(Object Graph)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ณต์žก์“ฐ..


๋งŒ์•ฝ์— ์ด๋Ÿฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ˜น์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ผ๋ถ€๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”? ์•„๋‹ˆ๋ฉด ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ์ „๋‹ฌํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ• ๋•Œ๋Š”์š”?

 

์šฐ๋ฆฌ๊ฐ€ ์ž˜ ์•Œ๊ณ  ์žˆ๋Š” Nib์ด๋‚˜ plist ํŒŒ์ผ์ด ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๊ฐ€ ํŒŒ์ผ๋กœ ์ €์žฅ๋œ ์ข‹์€ ์˜ˆ์‹œ์ธ๋ฐ์š”, Nib์€ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๊ฐ์ฒด๋“ค์˜ ๊ด€๊ณ„๋ฅผ ํŒŒ์ผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๊ณ , plist ํŒŒ์ผ๋„ ์–ด๋–ค ๊ฐ์ฒด์— ๋Œ€ํ•œ ๊ฐ’์˜ ๋ชฉ๋ก๋“ค์„ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๊ฐ€ Archive์™€ Serialization์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

 

์˜ค Nib์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค..! ์ปค์Šคํ…€ ๋ทฐ์˜ ์ƒ์„ฑ์ž์— NSCoder๊ฐ€ ๋‚˜์˜จ๊ฒƒ๊ณผ ๋ญ”๊ฐ€ ์—ฐ๊ด€์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”..! ๊ณ„์† ์ฝ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

Archives

์•„์นด์ด๋ธŒ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์•„์นด์ด๋ธŒ๋Š” ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋“ค์˜ ์‹๋ณ„์ž๋ฅผ ๋ณด์กดํ•˜๊ณ  ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค๊ณผ์˜ ๊ด€๊ณ„๋„ ๋ณด์กดํ•ฉ๋‹ˆ๋‹ค. ์•„์นด์ด๋น™ ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ธ์•„์นด์ด๋น™ํ•˜๋ฉด ๋ช‡๊ฐ€์ง€ ์˜ˆ์™ธ์ƒํ™ฉ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ์•„์นด์ด๋น™ํ•˜๊ธฐ ์ „ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋ณต์›(rebuilt)ํ•ฉ๋‹ˆ๋‹ค.

 

์•„์นด์ด๋ธŒ๋ฅผ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์˜ ์ €์žฅ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋”ฐ๋กœ ์ €์žฅ์„ ์œ„ํ•œ ํฌ๋งทํŒ…์„ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋ผ์„œ ์ด์ ์ด ์žˆ์ฃ . ์–ธ์•„์นด์ด๋น™์„ ํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ตฌ์š”.

 

์•„์นด์ด๋น™์ด ์ง€์›๋˜๋ ค๋ฉด ๊ฐ์ฒด๊ฐ€ ๋ฐ˜๋“œ์‹œ NSCoding ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

NSCoding ๋ฌธ์„œ๋ฅผ ์—ด์–ด๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐœ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ Required๋กœ ์•ˆ๋‚ด๋˜์–ด ์žˆ๋Š”๋ฐ์š”, ํ•˜๋‚˜๋Š” ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜๋“ค์„ ์•„์นด์ด๋น™ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์•„์นด์ด๋น™๋œ ๋ฐ์ดํ„ฐ์—์„œ ๋ณ€์ˆ˜์™€ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ณต์›ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

Foundation์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐ’ ํƒ€์ž… ๊ฐ์ฒด๋“ค์€ NSCoding์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. NSString, NSArray ๊ฐ™์€ ์นœ๊ตฌ๋“ค์ด์š”. ์ด๋Ÿฐ ํƒ€์ž…๋“ค์€ ๋”ฐ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์•„์นด์ด๋น™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Serializations

Serialization์€ ๊ฐ’ ๊ฐ์ฒด๋“ค์„ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์˜ˆ์š”. ๋”•์…”๋„ˆ๋ฆฌ, ๋ฐฐ์—ด, ๋ฌธ์ž์—ด, ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋“ค์„ ๊ณ„์ธต ๊ด€๊ณ„๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์ฃ . ์•„์นด์ด๋ธŒ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ง๋ ฌํ™”๋Š” ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ฐ’๊ณผ ๊ณ„์ธต ์ƒ์˜ ์œ„์น˜๋งŒ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์š”. ๊ทธ๋ž˜์„œ ๋งŒ์•ฝ ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๋งŽ์€ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์ฐธ์กฐ๋˜๊ณ  ์žˆ๋‹ค๋ฉด, ์ง๋ ฌํ™”๋œ ํŒŒ์ผ์—๋Š” ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ 1๋Œ€1 ๋งค์นญ์ด ๋˜๋Š” ๊ด€๊ณ„๋กœ ํ‘œํ˜„๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

plist๊ฐ€ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ธ๋ฐ์š”,

์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋“ค์˜ ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ, ๊ฐ์ฒด๋“ค๊ฐ„์˜ ๊ณ„์ธต๊ด€๊ณ„์™€ ๊ฐ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ณ ์žˆ๋Š” ๊ฐ’์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์ฃ .

 

๋นฝ ํˆฌ ๋” ํƒ‘

๋‹ค์‹œ ์ด ์ฝ”๋“œ๋กœ ๋Œ์•„์™€๋ณผ๊ฒŒ์š”. required init?(coder: NSCoder)๋Š” ์–ด๋–ค ์ผ์„ ํ• ๊นŒ์š”?

 

์•„๊นŒ ์•„์นด์ด๋น™๊ณผ ๋””์ŠคํŠธ๋ฆฌ๋ทฐ์…˜์ด ๊ฐ€๋Šฅํ•œ NSCoder๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ Coder์˜€์ฃ ? ๊ทธ๋Ÿผ ์ด ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์•„์นด์ด๋น™ํ•˜๊ฑฐ๋‚˜ ์–ธ์•„์นด์ด๋น™ํ•˜๊ฒ ๋„ค์š”.

 

๊ทธ๋Ÿผ ์ € ๋ฉ”์„œ๋“œ๋„ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ด์•ผ๊ฒ ์ฃ ?

 

๋ ˆ์ „๋“œ.. ์„ค๋ช…์ด ์—†์Šต๋‹ˆ๋‹คใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

๋‹คํ–‰์Šค๋Ÿฝ๊ฒŒ๋„ UIView ๋ฌธ์„œ์—๋Š” ์ ํ˜€์žˆ๋„ค์š”.. ์Šคํ† ๋ฆฌ๋ณด๋“œ๋‚˜ nib ํŒŒ์ผ์„ ํ†ตํ•ด ๋ทฐ๋ฅผ ๋กœ๋“œํ•˜๋ฉด์„œ ์ปค์Šคํ…€ํ•˜๊ฒŒ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•˜๋„ค์š”.

 

์Šคํ† ๋ฆฌ๋ณด๋“œ์™€ nib์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ทฐ๊ฐ€ ์–ด๋–ป๊ธธ๋ž˜ ์ด๋Ÿด๊นŒ์š”??

 

์ด๋ฒˆ์—” ์—ฌ๊ธฐ์„œ nib์˜ ๋ฌธ์„œ๋ฅผ ์—ด์–ด๋ดค์Šต๋‹ˆ๋‹ค..

 

 

์˜คํ‚ค์˜คํ‚ค~ ์‹ค๋งˆ๋ฆฌ๋ฅผ ์ฐพ์•˜๋„ค์š”! Nib ํŒŒ์ผ์€ ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋” ๋ฌธ์„œ๋ผ๊ณ  ํ•ด์š”. ์ด ๋ฌธ์„œ๊ฐ€ ์ˆ˜์ •๋˜๊ณ  ํŒŒ์ผ์ด ์ €์žฅ๋  ๋•Œ ๊ฐ์ฒด๊ทธ๋ž˜ํ”„๊ฐ€ ์ƒ์„ฑ๋˜์–ด์„œ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋ฉด ๊ฐ์ฒด๊ทธ๋ž˜ํ”„๊ฐ€ ์–ธ์•„์นด์ด๋น™ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

๋” ์ฝ์–ด๋ณด๋‹ˆ ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ทฐ๋“ค๋„ ๋ชจ๋‘ Nib์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์ •๋ฆฌํ•˜๋ฉด ์Šคํ† ๋ฆฌ๋ณด๋“œ๋‚˜ xib๋Š” ๊ฒฐ๊ตญ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„์นด์ด๋น™๋œ ํŒŒ์ผ์ด๊ณ , ์ด ํŒŒ์ผ์„ ๋กœ๋“œํ•ด์ฃผ์–ด์•ผ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š”๊ฑฐ์ฃ . ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ ๋ถˆ๋ฆฌ๋Š” ์ƒ์„ฑ์ž๊ฐ€ required init?(coder: NSCoder) ์ด์—ˆ๋˜ ๊ฒƒ์ด๋„ค์š”..!

 

์ž ๊น.. ๊ทธ๋Ÿผ awakeFromNib์€ ๋ญ”๋ฐ..?

๊ฐ‘์ž๊ธฐ ๋“œ๋Š” ์˜๋ฌธ.. ๊ทธ๋Ÿผ awakeFromNib์€ ์™œ ๋ถˆ๋ฆฌ๋Š”๊ฑธ๊นŒ์š”??

์ด๋ฒˆ์—๋„ ์—ญ์‹œ ๋ฌธ์„œ๊ฐ€ ์žˆ์—ˆ๋„ค์š”.

 

์ •๋ฆฌํ•ด๋ณด๋ฉด NSCoding ์„ ์ฑ„ํƒํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์–ธ์•„์นด์ด๋น™๋œ ์ดํ›„์— ์ดˆ๊ธฐํ™” ๋œ ์ดํ›„์— ๋ถˆ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ awakeFromNib์ด๋ผ๊ณ  ํ•ด์š”. ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ์ด๋ฃจ๋˜ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ธ์Šคํ„ด์Šคํ™” ๋˜๊ณ  ์ดˆ๊ธฐํ™”๋˜๋ฉด ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” outlet์ด๋‚˜ action์ด ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ดํ›„์— ๋ถˆ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ awakeFromNib์ž…๋‹ˆ๋‹ค.

 

๊ฐ์ฒด ๊ทธ๋ž˜ํ”„์— ์žˆ๋˜ ๊ฐ์ฒด๋“ค์ด ์–ธ์•„์นด์ด๋น™ ๋˜๊ณ  ์‹œ์ž‘๋˜๋Š” ์ˆœ์„œ๊ฐ€ ์•„์นด์ด๋น™๋œ ์ˆœ์„œ์™€ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ init ๋ฉ”์„œ๋“œ์—์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ํ–‰๋™์€ ์ ˆ๋Œ€ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ•ด์š”. awakeFromNib์€ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ดˆ๊ธฐํ™”๋œ ์ดํ›„์— ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜๋‹ค๊ณ  ํ•ด์š”.

 

๋งˆ๋ฌด๋ฆฌ

์ด๋ ‡๊ฒŒ ํ•ด์„œ NSCoder์™€ ๊ฐ์ฒด๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค! ์˜ˆ์ „์— ํ•œ ๋ฒˆ ๋“ค์—ˆ๋˜ ๋‚ด์šฉ์ธ๋ฐ ๋‹ค ์žŠ๊ณ ์žˆ๋‹ค๊ฐ€ ์ „์ฒด์ ์œผ๋กœ ๋ฌธ์„œ๋ฅผ ๋‹ค์‹œ ๋ณด๋‹ˆ ํ™•์‹คํžˆ ์ •๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค..!

 

๋ฌธ์„œ์— ๋งŽ์ด ์˜์ง€ํ•˜๋‹ค๋ณด๋‹ˆ ์ž˜๋ชป ํ•ด์„ํ•˜๊ฑฐ๋‚˜ ์ดํ•ดํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์–ด์š”.. ์ œ ๊ธ€์„ ๋„ˆ๋ฌด ๋งน์‹ ํ•˜์ง€ ๋ง์•„์ฃผ์‹œ๊ณ  ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”!

 

๋ž˜ํผ๋Ÿฐ์Šค

https://developer.apple.com/documentation/foundation/nscoding

https://developer.apple.com/documentation/foundation/nscoder

https://developer.apple.com/documentation/uikit/uiview/

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i

https://developer.apple.com/documentation/objectivec/nsobject/1402907-awakefromnib