Last active
April 18, 2018 14:46
-
-
Save baquaz/60c111650a8912e130bb4eb9f729d14a to your computer and use it in GitHub Desktop.
A class that saves and retrieves an array to the device
This file contains hidden or 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
| //: Playground - a class that saves and retrieves an array to the device | |
| import Foundation | |
| class ArrayStorageManager: NSObject { | |
| static let shared = ArrayStorageManager() | |
| private var documentsDirectoryURL: URL { | |
| let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first | |
| return url! | |
| } | |
| //MARK: - Custom Objects Array | |
| func saveObjectArray<T: NSCoding>(_ array: [T], key: String) { | |
| print("Save Array:\(array.debugDescription)") | |
| print("File name: \(key)") | |
| let file = documentsDirectoryURL.appendingPathComponent(key) | |
| NSKeyedArchiver.archiveRootObject(array, toFile: file.path) | |
| print("Saved array for path: \(file.path)") | |
| } | |
| func loadObjectArray(for key: String) -> Array<AnyObject>? { | |
| let file = documentsDirectoryURL.appendingPathComponent(key) | |
| let result = NSKeyedUnarchiver.unarchiveObject(withFile: file.path) | |
| return result as? Array | |
| } | |
| //MARK: - Strings Array | |
| func saveStringsArray(_ array: [String], forKey key: String) { | |
| UserDefaults.standard.set(array, forKey: key) | |
| UserDefaults.standard.synchronize() | |
| print("Saved (String) array: \(array.debugDescription)") | |
| } | |
| func loadStringArray(for key: String) -> [String]? { | |
| return UserDefaults.standard.array(forKey: key) as? [String] ?? nil | |
| } | |
| //MARK: - Int Array | |
| func saveIntArray(_ array: [Int], forKey key: String) { | |
| UserDefaults.standard.set(array, forKey: key) | |
| UserDefaults.standard.synchronize() | |
| print("Saved (Int) array: \(array.debugDescription)") | |
| } | |
| func loadIntArray(for key: String) -> [Int]? { | |
| return UserDefaults.standard.array(forKey: key) as? [Int] ?? nil | |
| } | |
| //MARK: - Bool Array | |
| func saveBoolArray(_ array: [Bool], forKey key: String ) { | |
| UserDefaults.standard.set(array, forKey: key) | |
| UserDefaults.standard.synchronize() | |
| print("Saved (Bool) array: \(array.debugDescription)") | |
| } | |
| func loadBoolArray(for key: String) -> [Bool]? { | |
| return UserDefaults.standard.array(forKey: key) as? [Bool] ?? nil | |
| } | |
| //Date Array | |
| func saveDateArray(_ array: [Date], forKey key: String ) { | |
| UserDefaults.standard.synchronize() | |
| UserDefaults.standard.set(array, forKey: key) | |
| print("Saved (Date) array: \(array.debugDescription)") | |
| } | |
| func loadDateArray(for key: String) -> [Date]? { | |
| return UserDefaults.standard.array(forKey: key) as? [Date] ?? nil | |
| } | |
| } | |
| class Person: NSObject, NSCoding { | |
| private struct Keys { | |
| static let Name = "name" | |
| static let Company = "company" | |
| static let Age = "age" | |
| } | |
| var name: String | |
| var company: String | |
| var age: Int | |
| init(name: String, company: String, age: Int) { | |
| self.name = name | |
| self.company = company | |
| self.age = age | |
| super.init() | |
| } | |
| //MARK: Coder | |
| func encode(with aCoder: NSCoder) { | |
| aCoder.encode(name, forKey: Keys.Name) | |
| aCoder.encode(company, forKey: Keys.Company) | |
| aCoder.encode(age, forKey: Keys.Age) | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| name = aDecoder.decodeObject(forKey: Keys.Name) as! String | |
| company = aDecoder.decodeObject(forKey: Keys.Company) as! String | |
| age = aDecoder.decodeInteger(forKey: Keys.Age) | |
| super.init() | |
| } | |
| } | |
| //////////////////////////////////////////////////////////////////////////////////////// | |
| //////////////////////////////////////////////////////////////////////////////////////// | |
| //EXAMPLE | |
| //////////////////////////////////////////////////////////////////////////////////////// | |
| let archiver = ArrayStorageManager.shared | |
| //MARK: Custom objects example | |
| let me = Person(name: "Piotr", company: "Softnauts", age: 27) | |
| let people: [Person] = [me] | |
| archiver.saveObjectArray(people, key: "ppl") | |
| if let objValues = archiver.loadObjectArray(for: "ppl") as? [Person] { | |
| print("First person name = \(String(describing: objValues.first!.name))") | |
| print("First person company = \(String(describing: objValues.first!.company))") | |
| print("First person age = \(String(describing: objValues.first!.age))") | |
| } | |
| //MARK: Strings example | |
| let myStrings = ["one", "two"] | |
| archiver.saveStringsArray(myStrings, forKey: "str") | |
| if let strValues = archiver.loadStringArray(for: "str") { | |
| print("Loaded strings array: \(strValues.debugDescription)") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment