[iOS] UMC iOS ์ธ๋ฏธ๋ 7์ฃผ ์ฐจ
๐ก
main thread๋ ํญ์ async ๋ฐฉ์์ผ๋ก ์ฌ์ฉํด์ผ ํ๋ค!
sync๋ฅผ ์ฌ์ฉํ๋ฉด dead-lock ์ํ๊ฐ ๋๋ค. (์ฑ์ด ์ฃฝ์ด๋ฒ๋ฆผ)
DB์ DBMS
DB(Data Base): ๋ฐ์ดํฐ๋ค์ ์งํฉ์ฒด
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค: ํ์ผ -> ํ ์ด๋ธ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ฒ ๋จ
UserDefaults
์๋ ๋ก๊ทธ์ธ์ ๊ตฌํํ ๋ ์์ฃผ ์ฌ์ฉํ๋ค
ex) ํค -> ๋ก๊ทธ์ธ์ ํ ์ ์ด ์๋์ง? / ๊ฐ -> ๊ฒฐ๊ณผ
- ํค์ ๊ฐ์ ์์ผ๋ก ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ค. (->๋์ ๋๋ฆฌ)
- ์ฑ์ ๊ป๋ค ์ผ๋ ๋ฐ์ดํฐ๊ฐ ๋ณด์กด๋๋ค.
๋จ์ : ํ๋์ key ๊ฐ์ ์ฌ๋ฌ value ์ ์ฅํด์ ํ๋ ๊บผ๋ด์ค์ง ๋ชปํจ 1๊ฐ์ key - 1๊ฐ์ value
ex) ์ ์ ์ ์ด๋ฆ, ์ ํ๋ฒํธ, ์ฃผ์, ... 3๊ฐ์ ์ ๋ณด๋ฅผ ๋ฐ๋ก ์ ์ฅํด์ ํ๋ํ๋ ๊บผ๋ด์ผ ํ๋ ๊ท์ฐฎ์ ์กด์ฌ
๋ฐ์ดํฐ ์ ์ฅ
UserDefaults.standard.set(password, forKey: name)
"์ด๋ฆ(key)" : "๋น๋ฐ๋ฒํธ(value)"
๋ฐ์ดํฐ ๊บผ๋ด์ค๊ธฐ
if UserDefaults.standard.string(forKey: name) == nil {
print("๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
} else if password != UserDefaults.standard.string(forKey: name) {
print("ํ๋ฆฐ ๋น๋ฐ๋ฒํธ์
๋๋ค.")
} else password == UserDefaults.standard.string(forKey: name) {
print("๋ก๊ทธ์ธ ์ฑ๊ณต!")
}
์ค์ต

๐ฅ (login)ViewController
//
// ViewController.swift
// Week7
//
// Created by yeonsu on 2022/11/07.
//
/* ๋์
๋๋ฆฌ ๊ฐ๋
// dictionary: ํค๋ฅผ ๋ฃ์ผ๋ฉด ๊ฐ์ด ๋์จ๋ค!
var dictionary = ["apple": "์ฌ๊ณผ", "banana" : "๋ฐ๋๋"]
print(dictionary["apple"]!)
dictionary["keyboard"] = "ํค๋ณด๋" //๋์
๋๋ฆฌ ์ถ๊ฐ
*/
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
// ๋ก๊ทธ์ธ ๋ฒํผ
@IBAction func loginButtonDidTap(_ sender: Any) {
guard let name = nameTextField.text else {return}
guard let password = passwordTextField.text else {return}
// ๋ฐ์ดํฐ ์ฝ์ด์ค๊ธฐ
if UserDefaults.standard.string(forKey: name) == nil {
print("๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
} else if password != UserDefaults.standard.string(forKey: name) {
print("ํ๋ฆฐ ๋น๋ฐ๋ฒํธ์
๋๋ค.")
} else if password == UserDefaults.standard.string(forKey: name) {
print("๋ก๊ทธ์ธ ์ฑ๊ณต!")
}
}
// ํ์๊ฐ์
๋ฒํผ
@IBAction func signUpButtonDidTap(_ sender: Any) {
// ๋ฒํผํด๋ฆญ ์ ํ์๊ฐ์
view๋ก ์ด๋
guard let signUpViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUpViewController") as? SignUpViewController else {return}
navigationController?.pushViewController(signUpViewController, animated: true)
}
}
๐ฅ SignUpViewController
//
// SignUpViewController.swift
// Week7
//
// Created by yeonsu on 2022/11/07.
//
import UIKit
class SignUpViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
// ํ์๊ฐ์
๋ฒํผ
@IBAction func signUpButtonDidTap(_ sender: Any) {
guard let name = nameTextField.text else {return}
guard let password = passwordTextField.text else {return}
// ๋ฐ์ดํฐ ์ ์ฅ
UserDefaults.standard.set(password, forKey: name)
// ๋ฐ์ดํฐ ์์ ํ์ธ
print("์ด๋ฆ: \(name), ๋น๋ฐ๋ฒํธ: \(password)")
navigationController?.popViewController(animated: true)
}
// ์ทจ์ ๋ฒํผ
@IBAction func cancelButtonDidTap(_ sender: Any) {
}
}

์์๋๋ฉด ์ข์ ๋ด์ฉ๋ค
- JSON์ด๋ ๋ฌด์์ผ๊น?
- ์๋ฒ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ iOS๋ ์ด๋ป๊ฒ ์ํตํ ๊น?
- UserDefaults๋ณด๋ค ๋ ์ ์ฉํ ์์ฒด DB๋ ์์๊น? (KeyChain, CoreData, Realm, FireBase)