iOS

[iOS] - UserDefaults

사과농사 2021. 10. 3. 15:07

UserDefaults

사용자의 기본 database이며 앱 실행 중에 key-value 값을 지속적으로 저장합니다

NSObject를 상속받습니다.

 

  • 주로 사용자 기본 설정의 값들을 저장합니다.
  • Set할 때 Any, floats, doubles, integers, Boolean values, and URLs. 타입을 기본적으로 제공합니다.
  • Collection타입도 제공합니다. Array, Dictionary, Data타입
  • NotificationCenter에서 didChangeNotification이 제공
  • removeObject(forKey: String)으로 제거 가능.
  • 파일 시스템의 위치로도 사용가능

 

특이한 점

  • float, doubles, integers, Boolean은 Value를 get할 때 key값이 없으면 0과 false를 반환한다.

 

정리

  • UserDefaults를 사용하면 앱 어느곳에서나 데이터를 쉽게 읽고 저장 가능.
  • UserDefaults는 사용자 기본 설정과 같은 단일 데이터 값에 적합.
  • UserDefaults는 [데이터, 키(key)]으로 데이터를 저장합니다.
  • standard로 싱글톤 사용
  • 대용량의 데이터를 저장할 때 사용하지 않고, 아이디 저장, 자동 로그인 여부, 환경설정 같은 단일 데이터들을 저장함

참고

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

 

Apple Developer Documentation

 

developer.apple.com