Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

save Codable in userdefaults and fetch codable from userdefaults ios swift

let userDataIdentifier = "SavedUserData"

///*Saves UserData as codable in UserDefaults*
func saveUserData(userData: YOUR_MODAL) {
    userDefault.set(try? PropertyListEncoder().encode(userData), forKey: userDataIdentifier)
}

///*Fetchs UserData as codable from UserDefaults*
func getUserData() -> YOUR_MODAL? {
    if let data = userDefault.value(forKey: userDataIdentifier) as? Data {
        return try? PropertyListDecoder().decode(YOUR_MODAL.self, from: data)
    }
    return nil
}

// YOUR_MODAL = Your Codable/Simple Structure or Class!
Comment

PREVIOUS NEXT
Code Example
Swift :: swift filter dictionary 
Swift :: swiftui list navigation link 
Swift :: how to check object is nil in swift 
Swift :: swift close app 
Swift :: swiftui circle 
Swift :: hide status bar in tableview cell in swift 
Swift :: get class name swift 
Swift :: power number in swift 
Swift :: swift collection view check if you are at the bottom 
Swift :: how to change background color of ui button swift 
Swift :: swift change background color 
Swift :: make preivew in dark mode swiftui 
Swift :: How to find index of list item in Swift? 
Swift :: swift add programmatically constraint to view 
Swift :: change image tint color swiftui 
Swift :: check enumatted arrray last item swift 
Swift :: swiftui textfield editable false 
Swift :: symfony swiftmailer 
Swift :: Swift Create an Empty Set 
Swift :: how to recieve hex value from NSData swift 
Swift :: Create a Swift Array 
Swift :: toggle button swift 
Swift :: two integer value sum in swift 
Swift :: swift session.input 
Swift :: how to debug before app launch swift 
Swift :: Swift Assigning and accessing a value from an optional 
Swift :: Swift Boolean Literals 
Swift :: customize change color size MDCActivityIndicator swift 
Swift :: Allow user to import image 
Swift :: swift connect wifi 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =