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 :: swiftui vstack alignment 
Swift :: xcode disable a button 
Swift :: remove back button text nav bar swift 
Swift :: swift uiview gradient 
Swift :: Swift Properties 
Swift :: how to disable uitableview scrolling in swift 
Swift :: swiftui full screen sheet 
Swift :: textchange in textview swift 
Swift :: how do change title color in navigation bar 
Swift :: swift create a method who can return result or throw an error 
Swift :: swift sleep milliseconds 
Swift :: how to style textfield swiftui 
Swift :: swift find difference between two arrays 
Swift :: swift add width/height constraint to view 
Swift :: add to beginning of array swift 
Swift :: swift make condition that you are in sumulator 
Swift :: imageliteral swiftui 
Swift :: check notification permission ios swift 
Swift :: how to get the path of selected PDF/doc from file manager in ios swift programmatically 
Swift :: swift set keyboard next functionality 
Swift :: how to include a library in swift 
Swift :: How to remove the last item from an array in swift 
Swift :: value to value in sum 
Swift :: Swift Static Properties 
Swift :: Swift Access Struct Properties Swift 
Swift :: get parent view controller in presentation mode swift 
Swift :: swift reading binary data 
Swift :: array of button listeners swift 
Swift :: Memberwise Initializer for structs Swift 
Swift :: add placeholder to code snippets xcode 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =