간단한 data 저장하기 (UserDefualts vs Custom Plist)
간단한 data를 저장하는 방법에 대해 알아보자 1. UserDefualts : 가장 간단한 data를 간단하게 입력하는 방법이다. 2. Custom Plist : 간단한 data를 직접 생성한 Plist 파일에 저장한다. UserDefualts 보다 조금더 데이터를 나누어서 관리하고자 할 때 적합한 것 같다. 앱을 최초 생성하면 info.plist가 있는데 이런 data(NSDictionary)를 만들수 있고 그 data를 읽을 수 있다. 또는 그냥 string으로 txt 파일로 저장할 수도 있다(저장하는 파일 이름에 따라). 간단하게 코드로 살펴보면 1. UserDefualts // Userdefualts 사용 //(1)저장할 장소 let userPath = UserDefaults.standard // (2)data 저장 userPath.set( "min" , forKey: "name" ) userPath.set([ 3 , 1 , 4 ], forKey: "array" ) userPath.synchronize() // (3)data 가져오기 let name = userPath.string(forKey: "name" ) let array = userPath.array(forKey: "array" ) Colored by Color Scripter cs (1) 저장할 곳의 이름을 설정한다. 저장장소는 한곳이라 class 함수로 지정된다. (2) 저장장소에 key값으로 data를 저장한다. 저장할 수 있는 data는 string,Int,Double,Base64 type, Bool, Array, Dictionary 등등을 저장할 수 있다. 이때 synchronize()로 동기화 하여 ...