ํ ์คํธ์ฉ JSON ํ์ผ์ ์ดํ์๋ ์ฌ์ฉํ์ง ์์ ๊ฒ์ด๊ธฐ์ test ํด๋์ ๋ฐ๋ก Asset catalog๋ฅผ ๋ง๋ค์ด์ ๊ทธ๊ณณ์์๋ง ๋ฃ์ด๋๊ณ ์ฌ์ฉํ๊ณ ์ถ์๋ค. ๋๋ฌธ์ ํด๋๋ฅผ ํ๋ ๋ง๋ค์ด์ ์ด๋ฆ์ Json.xcassets ์ผ๋ก ์ ์ธํ๊ณ ๊ทธ ์์ ํ์ผ์ ๋ฃ์๋ค. ๊ทธ๋ฐ๋ฐ asset catalog์ ๋ชจ์์ด ๊ธฐ์กด์ ๊ฒ๊ณผ๋ ๋ฌ๋๋ค. ํ์ธํด๋ณด๋ asset catalog๋ฅผ ๋ง๋ค๋๋ ๋จ์ํ๊ฒ ํด๋๋ฅผ ๋ง๋๋ ๊ฒ์ด ์๋๋ผ File → New → File์ ํ๊ณ ๋ค์ด๊ฐ Resource์ Asset Catalog๋ฅผ ์ ํํ์ฌ ๋ง๋ค์ด์ผ ํ๋ค.
๊ทธ ํ test code๋ฅผ ์์ฑํ์ฌ ๋๋ฆฌ๋๋ฐ ์ด๋ฒ์๋ NSDataAsset์ ๊ฐ์ ธ์ค๋ guard๋ฌธ์์ ๊ณ์ retrun์ผ๋ก ๋น ์ง๋ฉฐ ์ข ๋ฃ๋์๋ค.
guard let json = NSDataAsset(name: "box_office_sample") else {
return
}
์ง์ ์์ฑํ๋ Json asset ๋ง๊ณ main asset์ ํ์ผ์ ๋ฃ์ด๋ณด๋ ์ด๋ฒ์๋ ์ ๋๋ก binding ๋์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ bundle์ ๋ง๋ค์ด์ ์ค์ ํด์ฃผ์๋ค.
โ๏ธBundle
๋ฒ๋ค์ ๋ฌด์์ผ๊น? ๊ณต์๋ฌธ์์ ์ ์์์ ๋ฒ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค.
๐ก A representation of the code and resources stored in a bundle directory on disk.
๋์คํฌ์ ๋ฒ๋ค ๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋ ์ฝ๋ ๋ฐ resource์ ํํ
๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ UIImage๋ฅผ Asset์์ ์ฐพ์์ ๊ฐ์ ธ์ฌ ๋ ํญ์ ์ด ๋ฒ๋ค์ ์ด์ฉํ์๋ค. ๊ธฐ๋ณธ์ผ๋ก ์ค์ ๋์ด ์๋ main์์ ๊ฐ์ ธ์๊ธฐ์ ๋๋ฌ๋์ง ์์์ ๋ฟ! ํ์ํ resource๋ฅผ ์ฐพ์ผ๋ ค๋ฉด, ๋จผ์ ํด๋น resource๋ฅผ ํฌํจํ๋ ๋ฒ๋ค์ ์ง์ ํด์ผํ๋ค.
main Bundle
ํ์์์ main์ผ๋ก ์ง์ ๋์ด ์์ ๋ฟ์ด๋ค. ๊ธฐ๋ณธ ๋ฒ๋ค์ธ main์ ํ์ฌ ์คํ ์ค์ธ ์ฝ๋๋ฅผ ํฌํจํ๋ค. ๋ฐ๋ผ์ ์ฑ์์ ๊ธฐ๋ณธ ๋ฒ๋ค์ ์ฑ๊ณผ ํจ๊ป ์ ๊ณต๋๋ resource์ ๋ํ ์ ์ ๊ถํ์ ์ ๊ณตํ๋ค. ์ฑ์ด main ๋ฒ๋ค๊ณผ ์ง์ ์ํธ ์์ฉํ๋ ๊ฒฝ์ฐ๋ ๋ฐ๋ก ์์ฑํ ํ์๊ฐ ์์ง๋ง ํ์ํ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ฐ๋ฆฌ์ ํ๋ก์ ํธ์์์ ๊ฐ์ ์ํฉ์ด๋ค. ๋ณดํต ๋ฒ๋ค์ ์ฑ ํ๋ก์ ํธ ํ์ผ์ ๋ง๋ค๋ฉด ๊ธฐ๋ณธ์ผ๋ก ์์ฑ๋๋ค.
ํ์ผ | ์ค๋ช |
myApp | (ํ์)application code๊ฐ ํฌํจ๋ ์คํ ํ์ผ |
Application Icon | (ํ์/๊ถ์ฅ) application์ ๋ํ๋ด๊ธฐ ์ํ ์์ด์ฝ. |
Info.plist | (ํ์) ๋ฒ๋ค ID, ๋ฒ์ ๋ฒํธ, ํ์ ์ด๋ฆ ๋ฑ application์ ๋ํ ๊ตฌ์ฑ ์ ๋ณด |
MainWindow.nib | (๊ถ์ฅ) ์ฑ์ ๊ธฐ๋ณธ nib ํ์ผ. ์์ ์ ๋ก๋ํ ๊ธฐ๋ณธ interface ๊ฐ์ฒด |
Settings.bundle | ์ค์ application์ ์ถ๊ฐํ๋ ค๋ ์ฑ ๋ณ ๊ธฐ๋ณธ ์ค์ ์ ํฌํจํ๋ ํน์ํ ์ ํ์ ํ๋ฌ๊ทธ์ธ |
Custom resource ํ์ผ | localize resource X: ์ต์์ ๋๋ ํ ๋ฆฌ ๋ฐฐ์น localize resource O: ๋ฒ๋ค์ ์ธ์ด๋ณ ํ์ ๋๋ ํ ๋ฆฌ ๋ฐฐ์น Resource = nib files, images, sound files, configuration files, strings files, other custom data files |
init(for:)
์ฐ๋ฆฌ์ ํ ์คํธ ์ฝ๋์์ nil๋ก ๋ฐํ๋์๋ ๊ฒ๋ ์ด ๊ธฐ๋ณธ ์์ฑ๋ ๋ฒ๋ค์ ์๋ก ๋ง๋ custom resource ํ์ผ์ด ์ฌ๋ผ๊ฐ ์์ง ์๊ธฐ ๋๋ฌธ์ด์๋ค. ๋๋ฌธ์ ์ด resource ํ์ผ์ ํฌํจํ๋ ๋จ์๋ฅผ ๋ฒ๋ค ๋๋ ํ ๋ฆฌ๋ก ์ ์ฅํด ์ ๋ณด๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค.
let bundle = Bundle(for: JsonDecodeTests.self)
guard let json = NSDataAsset(name: "box_office_sample", bundle: bundle) else {
return
}
// Get the app's main bundle
let mainBundle = Bundle.main
// Get the bundle containing the specified private class.
let myBundle = Bundle(for: NSClassFromString("MyPrivateClass")!)
https://developer.apple.com/documentation/foundation/bundle/1417717-init
init(for:) | Apple Developer Documentation
Returns the object with which the specified class is associated.
developer.apple.com
์ด๋ init(for:)์ ์ฌ์ฉํ๋ค. ์ด๋ฅผ ์ด์ฉํ๋ฉด ์ง์ ํ class๊ฐ ์ฐ๊ฒฐ๋ NSBundle ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
init(for aClass: AnyClass)
โ๏ธLocating Resources in a Bundle
๋ฒ๋ค์ ์ฌ์ฉํ๋ฉด ๋ฒ๋ค ๋ด์ ํน์ resource์ ์์น๋ฅผ ๊ฐ์ ธ์จ๋ค. ์ด๋ฅผ ํตํด resource๋ฅผ ์ฐพ๋๋ฐ, ํน์ ํ์ ๋๋ ํ ๋ฆฌ์ ์๋ resource์ ๊ฒฝ์ฐ ํด๋น ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ ํ ์๋ ์๋ค. resource๋ฅผ ์ฐพ๊ณ ๋๋ฉด ๋ฒ๋ค์ ํ์ผ์ ์ด ์ ์๋ ๊ฒฝ๋ก ๋ฌธ์์ด์ด๋ URL์ ๋ฐํํ๋ค.
๋ฒ๋ค์ ๋์คํฌ์ resource๋ฅผ ์ฐพ์ ๋ ํน์ ํ ์์๋ฅผ ๋ฐ๋ผ ๊ฐ๋ค.
- Global (nonlocalized) resources
- Region-specific localized resources (based on the user’s region preferences)
- Language-specific localized resources (based on the user’s language preferences)
- Development language resources (as specified by the CFBundleDevelopmentRegion key in the bundle’s Info.plist file)
๋ฒ๋ค์ ์ด ์์๋๋ก resource๋ฅผ ์ฐพ๋๋ค. ์ด๋ 1๋ฒ์์ ์ฐพ๊ณ ๋๋ฉด 2๋ฒ, 3๋ฒ, 4๋ฒ์์๋ resource๋ฅผ ์ฐพ์ง ์๋๋ค. → ์๊ฐ ๋ญ๋น ์์ ์ ์ฑ๋ฅ์ ์ฌ๋ฆฌ๊ธฐ ์ํด.
โ๏ธUnderstanding Bundle Structures
๋ฒ๋ค ๊ตฌ์กฐ๋ ํ๋ซํผ๊ณผ ๋ฒ๋ค ์ ํ์ ๋ฐ๋ผ ๋ค๋ฅด๋ค. ๋๋ถ๋ถ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ์จ๊ธฐ๊ณ , ๋ณดํต ๋ฒ๋ค์์ resource๋ ์๋์ผ๋ก ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์ฐพ๊ณ ์๋ ค์ง ์์น์์ ์ฐพ์์ ธ์ load ๋๋ค. ๋ํ ๋ฒ๋ค ํด๋์ค์ ๋ฉ์๋์ ์์ฑ์ ์ฌ์ฉํ์ฌ ์๋ ค์ง ๋ฒ๋ค ๋๋ ํ ๋ฆฌ์ ์์น๋ฅผ ๊ฐ์ ธ์ค๊ณ ํด๋น ๋๋ ํ ๋ฆฌ์์ ๊ตฌ์ฒด์ ์ผ๋ก resource๋ฅผ ๊ฒ์ํ ์ ์๋ค.
→ ์ฐ๋ฆฌ ํ๋ก์ ํธ์ ๊ฒฝ์ฐ๋ ์ด๋ฅผ ํ์ฉํ ๊ฒ์ด๋ค. ํ ์คํธ ํด๋์ค๊ฐ ์๋ ๋๋ ํ ๋ฆฌ์ ์์น๋ฅผ ๊ฐ์ ธ์ค๋ฉด ์ด ๋๋ ํ ๋ฆฌ ๋ด๋ถ์ ์์ ์ผ๋ก ๋ง๋ค์ด์ ธ ์๋ json ํ์ผ์ ๊ฐ์ ธ์ฌ ์ ์์๋ค.