Apple Developer Documentation

Hello world

//set up
let userDefaults = UserDefaults.standard

...

self.userDefaults.set(self.itemArray, forKey: "TodoListArray")

//로드 파일
in didFinishLaunchingWithOptions
//how to know document path
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)

/Users/jaeyounglee/Library/Developer/CoreSimulator/Devices/BD6619AC-D182-4D9D-B91A-0C91FFCA2F0B/data/Containers/Data/Application/E9BC6709-12B1-487B-8471-996AC5E310B0/Documents
document .. library/preferences

...
in viewDidLoad

if let items = userDefaults.array(forKey: "TodoListArray") as? [String] {
            itemArray = items
        }

  1. User default 셋팅
  2. 키 - 밸류 값으로 (plist)로 이루어져서 파일이 만들어짐.
  3. 만들어진 파일 확인
  4. 로드

<aside> 💡 uuid = identity device unique id, simulator or physical

</aside>

언제 User Defaults를 사용하면 좋을까?

다양한 타입들을 {키:밸류}값으로 저장할 수 있고 나중에 불러올 수 있다.

예를 들면, 게임의 볼륨을 사용자가 off해놓았을 때, 다시 앱을 킬 때에는 off 상태로 켜진다.

즉, 환경 설정에서 사용자가 설정했던 값들은 유저디폴트로 저장할 수 있다. (보안과 상관없고 서버에 저장할 이유도 없는 친구들)

but, 유저디폴트는 매우 플렉서블하고 어댑터블한데 남용하면 안되는 이유는 사용가능한 용량이 매우 작기 때문

그래서 어레이나 딕셔너리를 유저디폴트로 처리하는 것은 추천하지 않는다.

유저디폴트는 데이터베이스가 아니기 때문에 db처럼 사용해서는 안된다.

싱글톤패턴이기 때문에 어디서나 접근가능하단 점은 매력적인 요소 중 하나

처음에 로드 되기때문에 속도에도 좋은 영향을 미칠 수 있음