새소식

iOS/Swift

[Swift] UserDefault

  • -

UserDefault


  • 간단한 데이터를 앱 내에 저장하기 위한 메커니즘

  • 앱의 설정, 환경 설정, 사용자 기본값 등과 같은 데이터를 유지하는 데 사용됩니다.

    어느 곳에서나 어디든지 쉽게 읽고 저장 가능

  • key-value 쌍으로 키값을 저장
let userDefaults = UserDefaults.standard

// value 추가
userDefaults.setValue("a", forKey: "test1") // a

// 삭제
userDefaults.value(forKey: "test1") // nil

더 많은 사용 용도는 공식문서 참고

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

 

UserDefaults | Apple Developer Documentation

An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app.

developer.apple.com

 

 

synchronize()


- UserDefaults 의 비동기 업데이트를 기다렸다가 성공적으로 저장되면 true, 그렇지 않으면 false return

 

 

https://developer.apple.com/documentation/foundation/userdefaults/1414005-synchronize

 

synchronize() | Apple Developer Documentation

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.

developer.apple.com

 

728x90

'iOS > Swift' 카테고리의 다른 글

[Swift] @discardableResult  (0) 2023.07.26
[Swift] Date / DateFormatter  (0) 2023.07.15
[Swift] Task, await, async  (0) 2023.05.20
[Swift] NotificationCenter  (0) 2023.04.16
[Swift] Localizable  (0) 2023.03.18
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.