Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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!
 
PREVIOUS NEXT
Tagged: #save #Codable #userdefaults #fetch #codable #userdefaults #ios #swift
ADD COMMENT
Topic
Name
1+1 =