포스트

UserDefaults를 사용하는 방법

iOS에서는 사용자 설정 같이 정보량이 적은 데이터들을 앱이 설치되어 있는 동안 저장하여 사용할 수 있습니다. 바로 UserDefaults를 사용해서 저장하는 방법입니다. UserDefaults는 integer, boolean, string, array, dictionary, URL 같은 타입들을 저장할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
let defaults = UserDefaults.standard
defaults.set(30, forKey: "Age")
defaults.set("JooHee", forKey: "Name")
defaults.set(false, forKey: "NotificationAllowed")

let array = ["Apple", "Banana"]
defaults.set(array, forKey: "Fruits")

let dict = ["Name": "Joohee", "Country": "KOR"]
defaults.set(dict, forKey: "UserInfoDict")


이와 같이 데이터들을 저장하면 영구적으로 저장이 됩니다. 즉, 앱을 다시 켰을 때도 저장했을 때와 똑같은 정보가 저장되어 있습니다. 저장한 데이터들을 다시 읽을 때도 쉽게 할 수 있습니다.

1
2
3
let age = defaults.integer(forKey: "Age")
let isNotificationAllowed = defaults.bool(forKey: "NotificationAllowed")
let name = defaults.string(forKey: "Name")


다만, 만약 저장되어 있는 데이터가 없다면 UserDefaults에서는 default value를 리턴합니다. 실제로 저장한 데이터와 기본값을 구분하기 위해서 어떤 기본값을 리턴하는지 유의해서 사용해야 합니다.

  • integer(forKey:): returns 0
  • double(forKey:): returns 0
  • bool(forKey:)* returns false
  • string(forKey:): returns nil
  • object(forKey: String): returns Any?


위 string과 object 경우에는 옵셔널한 값이 리턴됩니다. 옵셔널 값을 받으면 원하는 데이터 타입으로 타입 변환해서 사용해야 합니다. 비어 있는 값이면 기본적으로 사용할 값을 ?? 연산자(nil coalescing operator)를 이용해서 지정합니다.

1
let savedFruits = defaults.object(forKey: "Fruits") as? [String] ?? [String]()


UserDefaults에 저장된 값을 지우고 싶을 때는 removeObject(forKey:) 메소드를 사용합니다.

1
defaults.removeObject(forKey: "Age")


UserDefaults는 싱글톤이고 thread-safe입니다.


참고

https://developer.apple.com/documentation/foundation/userdefaults

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.