Created
June 18, 2018 18:10
-
-
Save yjsoon/f161d45dae7e2d447927aa93dbffe000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func saveToFile(friends: [Friend]) { | |
let plistName = "friends" | |
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
let archiveURL = documentsDirectory.appendingPathComponent(plistName).appendingPathExtension("plist") | |
let propertyListEncoder = PropertyListEncoder() | |
let encodedFriends = try? propertyListEncoder.encode(friends) | |
try? encodedFriends?.write(to: archiveURL, options: .noFileProtection) | |
} | |
static func loadFromFile() -> [Friend]? { | |
let plistName = "friends" | |
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
let archiveURL = documentsDirectory.appendingPathComponent(plistName).appendingPathExtension("plist") | |
let propertyListDecoder = PropertyListDecoder() | |
guard let retrievedFriendsData = try? Data(contentsOf: archiveURL) else { return nil } | |
guard let decodedFriends = try? propertyListDecoder.decode(Array<Friend>.self, from: retrievedFriendsData) else { return nil } | |
return decodedFriends | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment