๐Ÿ—„Constants ๊ด€๋ฆฌํ•˜๊ธฐ

sangheeยท2021๋…„ 8์›” 20์ผ
0

๐ŸšฉiOS

๋ชฉ๋ก ๋ณด๊ธฐ
10/18
post-thumbnail
post-custom-banner

๐Ÿ—„Constants

Constants ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„์—, ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ๋ณด๊ด€ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด, Firebase์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ document์— ๋„ฃ๋Š” data์˜ ํƒ€์ž…์ด [String: Any]์ด๋‹ค. ์—ฌ๊ธฐ์„œ String์€ ํ‚ค๊ฐ’์ด๋‹ค. ์ด ํ‚ค ๊ฐ’์„ ๋งค๋ฒˆ ์ž‘์„ฑํ•˜๋‹ค ์ฒ ์ž๋ฅผ ํ‹€๋ฆฌ๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋”ฐ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค.

1. let

์ƒ์ˆ˜๋Š” ๊ด€๋ก€์ ์œผ๋กœ ์˜๋ฌธ ๋Œ€๋ฌธ์ž ์Šค๋„ค์ดํฌ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.

let KEY_PROFILE_IMAGE_URL = "profileImageUrl"

ํ”„๋กœํ•„ ์‚ฌ์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

let data: [String: Any] = [KEY_PROFILE_IMAGE_URL: imageUrl]

2. enum

์—ด๊ฑฐํ˜•์€ ๊ด€๋ จ๋œ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน์„ ๊ณตํ†ต์˜ ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•ด ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•˜๊ฒŒ ๋œ๋‹ค. enum ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์—ด๊ฑฐํ˜•์„ ์ •์˜ํ•œ๋‹ค.

enum DatabaseKeys: String {
    case profileImageUrl = "profileImageUrl"
}

DatabaseKeys.profileImageUrl์€ Dictionary keyํƒ€์ž…์ด๋ฏ€๋กœ .rawValue๋ฅผ ๋ถ™์—ฌ ์›์‹œ๊ฐ’์œผ๋กœ ๋„ฃ์—ˆ๋‹ค.

let data: [String: Any] = [DatabaseKeys.profileImageUrl.rawValue: imageUrl]

rawValue

Apple Developer Documentation - rawValue

The corresponding value of the raw type.

3. struct

enum์€ rawValue๋ฅผ ๊ณ„์† ์จ์•ผํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ๊ธฐ์— ํ•„์š”ํ•œ ํ•ญ๋ชฉ์—๋งŒ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

struct Const {
    struct DatabaseKey {
        static let profileImageUrl = "profileImageUrl"
    }  
    ...
}

Const์˜ DatabaseKey์˜ profileImageUrl์„ ๊ฐ€์ ธ์˜จ๋‹ค.

let data: [String: Any] = [Const.DatabaseKey.profileImageUrl: imageUrl]

๊นƒํ—ˆ๋ธŒ ์ปค๋ฐ‹ ์ฃผ์†Œ

GitHub - Commit

์ฐธ๊ณ  ์‚ฌ์ดํŠธ ์ฃผ์†Œ

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

profile
๐Ÿ‘ฉโ€๐Ÿ’ป
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€