Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

save codable in 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 :: Fetch structure from userdefaults ios swift 
Swift :: swift filter dictionary 
Swift :: swiftui scrollview hide scrollbar 
Swift :: swipe to delete xcode 
Swift :: Properties Swift 
Swift :: swift change status bar color 
Swift :: swiftui steppers 
Swift :: swift read file 
Swift :: dark mode change immediately swift 
Swift :: unit testing swift 
Swift :: link swiftui 
Swift :: uitableview set space between cells 
Swift :: string to json swift 
Swift :: swift hello world 
Swift :: switch case in swift language 
Swift :: swift uitextfield placeholder color 
Swift :: Swift Closure That Returns Value 
Swift :: swift inheritance 
Swift :: convert uiimage to swiftui image 
Swift :: accessing tab bar item action swift 
Swift :: debounce in swift 
Swift :: Swift "Hello, World!" Program 
Swift :: two value sum in swift 
Swift :: Swift Objects 
Swift :: Customize indicator view iOS swift 
Swift :: var i = 2 repeat { i *= i * 2 } while i < 100 print(i) 
Swift :: Button on right side/view of UITextField 
Swift :: swift constants 
Swift :: xcode macosx have view resize when window size changed 
Swift :: Swift Closed Range 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =