Skip to content

quicklearner4991/Codable-Database-Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Codable-Database-Swift

Xcode 12.4 Swift 5

Example of Codable data to show list of items and option to add items

Sample code

let dataPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Item.plist")

func saveItems(){
    let encoder = PropertyListEncoder()
    do {
    let data = try encoder.encode(self.array)
        try data.write(to: self.dataPath!)
    } catch {
        print(error)
    }
    self.tableView.reloadData()
}
func loadItems(){
    if let data = try? Data(contentsOf: dataPath!){
        let decoder = PropertyListDecoder()
        do {
        array = try decoder.decode([ToDoItem].self, from: data)
        }
        catch{
            print(error)
        }
        
    }
}