๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] UserDefaults์— ๊ฐ์ฒด ์ €์žฅํ•˜๊ธฐ (Encode & Decode Object in UserDefaults Data )

by Fomagran ๐Ÿ’ป 2021. 2. 26.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ UserDefaults์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ๊ฑด๋ฐ์š”.

 

์ €๋Š” UserDefaults์— ๋ชจ๋ธ ์ž์ฒด๋ฅผ ๋„ฃ๊ณ  ์ €์žฅํ•˜๊ณ  ์‹ถ์€๋ฐ  ๊ทธ๊ฒŒ ์ž˜ ์•ˆ๋˜๋”๋ผ๊ตฌ์š”...

 

๊ทธ๋ž˜์„œ ์—ด์‹ฌํžˆ ๊ตฌ๊ธ€๋งํ•ด์„œ ์•Œ๊ฒŒ๋œ ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”!


 

Food

 

์ €๋Š” ์ œ๊ฐ€ ๋จน์—ˆ๋˜ ์Œ์‹์˜ ์ด๋ฆ„,๊ฐ€๊ฒฉ,๋ง›์žˆ์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์Œ์‹์ด๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ์š”!

 

Codable์€ UserDefaults์— ์ €์žฅํ•˜๊ณ  ๋นผ์˜ค๊ธฐ ์œ„ํ•ด์„œ Encode,Decode ๊ณผ์ •์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฑ„ํƒํ•˜์˜€๊ณ 

 

Equatable์€ Food๊ฐ์ฒด๋ฅผ ์„œ๋กœ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์ฑ„ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import Foundation

struct Food:Codable,Equatable{
    let name:String
    let price:Int
    let isDelicious:Bool
}

FoodViewController

 

UserDefaults ์ธ์ฝ”๋”ฉ

 

์ด์ œ ViewController๋กœ ์ด๋™ํ•ด์„œ userDefaults์™€ Food๋ฅผ ๋‹ด์„ allFoods๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

   let userDefaults = UserDefaults.standard
   var allFoods:[Food]?

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ์Œ์‹์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ Food๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” saveAllFoods ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

๋งŒ์•ฝ allFoods์— ์•„๋ฌด๊ฒƒ๋„ ์—†๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” food๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€์•Š๋‹ค๋ฉด allFoods์—  append  ํ•ด์ค๋‹ˆ๋‹ค.

 

์•„๋‹ˆ๋ผ๋ฉด  allFoods์— food๋งŒ ๋‹ด๊ธด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

    private func saveAllFoods(food:Food) {
        if allFoods != nil {
            if !allFoods!.contains(food) {
                allFoods!.append(food)
            }
        }else{
            allFoods = [food]
        }
        userDefaults.setValue(try? PropertyListEncoder().encode(allFoods!), forKey: "Foods")
    }

๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ค์Œ ์ค„์— ์ค‘์š”ํ•œ๊ฒŒ ๋‚˜์˜ค๋Š”๋ฐ์š”. ๋ฐ”๋กœ  PropertyListEncoder์ž…๋‹ˆ๋‹ค.

 

์• ํ”Œ ๋ฌธ์„œ์— ๋“ค์–ด๊ฐ€์„œ ํ•ด์„ํ•ด๋ณด๋‹ˆ ๊ฐ์ฒด๋“ค์„ property list๋กœ encodingํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ๋˜์–ด์žˆ๋„ค์š”.

 

PropertyListEncoder๋ฅผ  ์‚ฌ์šฉํ•ด์„œ allFoods๋ฅผ ์ธ์ฝ”๋”ฉํ•ด์ค€ ๋’ค userDefaults์— ๊ธฐ์–ตํ•ด๋‘˜ ํ‚ค๋ฅผ ์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ์Œ์‹ ๊ฐ์ฒด๋“ค์„ allFoods์— appendํ•˜๊ณ  allFoods์•ˆ์—  ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋ชจ๋‘ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

    private func addFood() {
        
        let ์งœ์žฅ๋ฉด = Food(name: "์งœ์žฅ๋ฉด", price: 6000, isDelicious: true)
        let ์ƒ๋Ÿฌ๋“œ = Food(name: "์ƒ๋Ÿฌ๋“œ", price: 4500, isDelicious: false)
        let ์†Œ๊ณ ๊ธฐ = Food(name: "์†Œ๊ณ ๊ธฐ", price: 25000, isDelicious: true)
        
        allFoods = [์งœ์žฅ๋ฉด,์ƒ๋Ÿฌ๋“œ,์†Œ๊ณ ๊ธฐ]
        
        for food in allFoods!{
            saveAllFoods(food: food)
        }
        
    }

UserDefaults Decode

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ userDefaults์•ˆ์— ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๊บผ๋‚ด๋ด์•ผ๊ฒ ์ฃ ?

 

์ธ์ฝ”๋”ฉํ•  ๋•Œ์™€ ๊ฐ™์ด PropertyListDecoder๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋””์ฝ”๋”ฉํ•ด์ค๋‹ˆ๋‹ค.

 

Food๊ฐ์ฒด๊ฐ€ ๋‹ด๊ธด allFoods๋ผ๋Š” Array๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ Array<Food>.self๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”.

    private func loadAllFoods() {
        if let data = userDefaults.value(forKey: "Foods") as? Data {
            allFoods = try? PropertyListDecoder().decode(Array<Food>.self,from: data)
        }
    }

 

๊ทธ๋Ÿผ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ณผ๊นŒ์š”?

 

๋จผ์ € addFood๋ฅผ ์‹คํ–‰์‹œ์ผœ์„œ ์Œ์‹๋“ค์„ UserDefaults์— ์ €์žฅํ•ด์ค€ ๋’ค

   override func viewDidLoad() {
        super.viewDidLoad()
        addFood()
    }

์•ฑ์„ ๋„๊ณ  ๋‚˜์„œ ๋‹ค์‹œ loadAllFoods๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค€ ๋’ค allFoods๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด

    override func viewDidLoad() {
        super.viewDidLoad()
        loadAllFoods()
        print(allFoods)
    }

์ €์žฅํ•œ ์งœ์žฅ๋ฉด,์ƒ๋Ÿฌ๋“œ,์†Œ๊ณ ๊ธฐ ์ด๋ ‡๊ฒŒ Food๊ฐ์ฒด๊ฐ€ ์ž˜ ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€