์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[ Swift ] CRUD์™€ UserDefaults / ๊ฐ„๋‹จํ•œ ๋ฉ”๋ชจ ์•ฑ ๋งŒ๋“ค๊ธฐ

๐ŸŽ iOS/Swift

by AHN.Jihyeon 2024. 7. 14. 15:29

๋ณธ๋ฌธ

๐ŸŒŸ Create / Update

UserDefaults.standard.set()

 

 

 

 

๐ŸŒŸ Read

//String ํƒ€์ž…
UserDefaults.standard.string(forKey: "")

//Bool ํƒ€์ž…
UserDefaults.standard.bool(forKey: "")

//Int ํƒ€์ž…
UserDefaults.standard.integer(forKey: "")

 

 

 

 

๐ŸŒŸ Delete

UserDefaults.standard.removeObject(forKey: "")

 

 

Int, String ๊ณผ ๊ฐ™์€ ์›์‹œํƒ€์ž… ์•„๋‹Œ Struct๋‚˜ Class ํƒ€์ž…์„ ์ €์žฅํ•˜๋ ค๋ฉด json ์ธ์ฝ”๋”ฉ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. 

 

 

 

 

 


๐Ÿ“ UserDefaults๋ฅผ ํ™œ์šฉํ•œ ๊ฐ„๋‹จ ํฌ์ŠคํŠธ์ž‡ ์•ฑ ๋งŒ๋“ค๊ธฐ

 

๐Ÿ’ก ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•œ ์ดˆ๊ธฐํ™”

ํด๋กœ์ € ๋‚ด์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์„ค์ •ํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐํ™”์™€ ์„ค์ •์„ ํ•œ ๊ณณ์—์„œ ๊น”๋”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๋Ÿฌ ์†์„ฑ์„ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.

 

 

 

import UIKit
import SnapKit

class ViewController: UIViewController {
    
    private let label: UILabel = {
        let label = UILabel()
        label.text = "Postit"
        label.textColor = .black
        label.font = .boldSystemFont(ofSize: 30)
        return label
    }()
    
    private let textView: UITextView = {
        let textView = UITextView()
        textView.text = UserDefaults.standard.string(forKey: "memo")  //UserDefaults์—์„œ ๊ฐ’ read
        textView.backgroundColor = .systemGray3
        textView.layer.cornerRadius = 10
        textView.font = .boldSystemFont(ofSize: 24)
        return textView
    }()
    
    private lazy var saveButton: UIButton = {
       let button = UIButton()
        button.setTitle("Save", for: .normal)
        button.backgroundColor = .systemBlue
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .boldSystemFont(ofSize: 24)
        button.layer.cornerRadius = 10
        button.addTarget(self, action: #selector(SaveButtonTapped), for: .touchDown)
        return button
    }()
    
    private lazy var deleteButton: UIButton = {
       let button = UIButton()
        button.setTitle("Delete", for: .normal)
        button.backgroundColor = .systemGreen
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .boldSystemFont(ofSize: 24)
        button.layer.cornerRadius = 10
        button.addTarget(self, action: #selector(deleteButtonTapped), for: .touchDown)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureUI()
    }
    
    private func configureUI() {
        [label, textView, saveButton, deleteButton].forEach{ view.addSubview($0) }
        
        view.backgroundColor = .white
        
        label.snp.makeConstraints {
            $0.top.equalToSuperview().offset(100)
            $0.centerX.equalToSuperview()
        }
        
        textView.snp.makeConstraints {
            $0.top.equalTo(label.snp.bottom).offset(50)
            $0.centerX.equalToSuperview()
            $0.width.equalTo(300)
            $0.height.equalTo(450)
        }
        
        deleteButton.snp.makeConstraints {
            $0.top.equalTo(textView.snp.bottom).offset(50)
            $0.leading.equalToSuperview().inset(70)
            $0.width.equalTo(100)
            $0.height.equalTo(50)
        }
        
        saveButton.snp.makeConstraints {
            $0.top.equalTo(textView.snp.bottom).offset(50)
            $0.trailing.equalToSuperview().inset(70)
            $0.width.equalTo(100)
            $0.height.equalTo(50)
        }
    }
    
    
    @objc
    private func SaveButtonTapped(){
        //textView์— ์ž…๋ ฅ๋œ ๋ฉ”๋ชจ ์ €์žฅ
        UserDefaults.standard.setValue(textView.text, forKey: "memo")
        print("์ €์žฅ ์™„๋ฃŒ")
    }
    
    @objc
    private func deleteButtonTapped(){
        //textView์— ์ €์žฅ๋œ ๋ฉ”๋ชจ ์‚ญ์ œ
        UserDefaults.standard.removeObject(forKey: "memo")
        textView.text = ""
        print("์‚ญ์ œ ์™„๋ฃŒ")
    }
}

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ