Search code examples
swiftswift4nsuserdefaults

Complex Custom Object stored in NSUserDefaults. Codable Solution Swift 4


I have been reading and searching around for a while about how to store a Custom Object into "NSUserDefaults". So far I have got solutions that allow me to achieve that by implementing "NSCoding" into my custom object. The examples I have got are based on very simple Objects but, in my case, I am facing this challenge into an existing custom class that has a complex structure and with other custom class in it.

class MyCustomClass:NSObject, NSCoding{

   let codingTagSecondClass = "codingTagSecondClass"
   var mySecondClass:MySecondCustomClass?
   ...

   let codeingTagaString = "codeingTagaString"
   var aString = "aString"
}

And I have implemented the NSCoding methods like:

required init?(coder aDecoder: NSCoder) {
   aString = aDecoder.decodeObject(forKey: codeingTagaString) as! String
   mySecondClass = aDecoder.decodeObject(forKey: codingTagSecondClass) as? mySecondClass:MySecondCustomClass
}

func encode(with aCoder: NSCoder) {

   aCoder.encode(aString, forKey: codeingTagaString)
   aCoder.encode(mySecondClass, forKey: codingTagSecondClass)

}

and that is how I stored into NSUserDefaults

let archivedObject = NSKeyedArchiver.archivedData(withRootObject: myCustomObject!)
let defaults = UserDefaults.standard
defaults.set(archivedObject, forKey: defaultUserCurrentServerProxy)

This implementation only works for String var but it crashes when I try to do it with my secondCustomClass...

I can Imagine that is because "MySecondCustomClass" does not implement "NSCoding". is that correct? Is there a different way to achieve what I am trying to do? My Custom class has an structure larger than the one I show here so before I got Into coding or thinking a diferent alternative I need to know.

Thank you very much.


Solution

  • I am going to give a working example with a custom helper I have implemented for managing Path for different Codable objects

    My Class:private class StoredMediaItem:Codable{
        var metadata: String?
        var url: URL?
        var trackID:UInt32 = 0
    
        init(metadata:String?, url:URL?, trackID:UInt32){
    
            self.metadata = metadata
            self.url = url
            self.trackID = trackID
    
        }
    
    }
    

    for reading:

    func readStoredItemArray(fileName:String)->[StoredMediaItem]?{
    
        do {
            let storedMediaItemArray = try StorageHelper.retrieve(fileName, from: .caches, as: [StoredMediaItem].self)
    
            print("\(logClassNameOH) Read Stored Item Array from \(fileName) SUCCESS")
    
            return storedMediaItemArray
    
            } catch {
    
                print("\(logClassNameOH) Read Stored Item Array from\(fileName) ERROR -> \(error)")
                return nil
    
            }
    
    }
    

    for writing:

    private func saveMediaItemArray(_ mediaItemArrayTemp:[storedMediaItemArray], as fileName:String){
    
        do {
    
            try StorageHelper.store(storedMediaItemArray, to: .caches, as: fileName)
    
            print("\(logClassNameOH) Read Stored Item Array from \(fileName) SUCCESS")
    
        } catch {
            print("\(logClassNameOH) save MediaItem Array ERROR -> \(error)")
        }
    
    }
    

    And My StorageHelper:

    class StorageHelper{
    
        //MARK: - Variables
        enum StorageHelperError:Error{
            case error(_ message:String)
        }
    
        enum Directory {
            // Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud.
            case documents
    
            // Data that can be downloaded again or regenerated should be stored in the <Application_Home>/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
            case caches
        }
    
    
    
        //MARK: - Functions
        /** Store an encodable struct to the specified directory on disk
        *  @param object      The encodable struct to store
        *  @param directory   Where to store the struct
        *  @param fileName    What to name the file where the struct data will be stored
        **/
        static func store<T: Encodable>(_ object: T, to directory: Directory, as fileName: String) throws {
    
            let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    
            let encoder = JSONEncoder()
            do {
                let data = try encoder.encode(object)
                if FileManager.default.fileExists(atPath: url.path) {
                    try FileManager.default.removeItem(at: url)
                }
                FileManager.default.createFile(atPath: url.path, contents: data, attributes: nil)
            }
            catch {
                throw(error)
            }
    
        }
    
        /** Retrieve and convert an Object from a file on disk
        *  @param fileName    Name of the file where struct data is stored
        *  @param directory   Directory where Object data is stored
        *  @param type        Object type (i.e. Message.self)
        *  @return decoded    Object model(s) of data
        **/
        static func retrieve<T: Decodable>(_ fileName: String, from directory: Directory, as type: T.Type) throws -> T{
            let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    
            if !FileManager.default.fileExists(atPath: url.path) {
                throw StorageHelperError.error("No data at location: \(url.path)")
            }
    
            if let data = FileManager.default.contents(atPath: url.path) {
                let decoder = JSONDecoder()
                do {
                    let model = try decoder.decode(type, from: data)
                    return model
                } catch {
                    throw(error)
                }
            }
            else {
                throw StorageHelperError.error("No data at location: \(url.path)")
            }
        }
    
        /** Remove all files at specified directory **/
        static func clear(_ directory: Directory) throws {
    
            let url = getURL(for: directory)
            do {
                let contents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
                for fileUrl in contents {
                    try FileManager.default.removeItem(at: fileUrl)
                }
            }
            catch {
                throw(error)
            }
    
        }
    
        /** Remove specified file from specified directory **/
        static func remove(_ fileName: String, from directory: Directory) throws {
            let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
            if FileManager.default.fileExists(atPath: url.path) {
                do {
                    try FileManager.default.removeItem(at: url)
                } catch {
                    throw(error)
                }
            }
        }
    
    
        //MARK: Helpers
        /** Returns BOOL indicating whether file exists at specified directory with specified file name **/
        static fileprivate func fileExists(_ fileName: String, in directory: Directory) -> Bool {
    
            let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    
            return FileManager.default.fileExists(atPath: url.path)
    
        }
    
        /** Returns URL constructed from specified directory **/
        static fileprivate func getURL(for directory: Directory) -> URL {
    
            var searchPathDirectory: FileManager.SearchPathDirectory
    
            switch directory {
            case .documents:
                searchPathDirectory = .documentDirectory
            case .caches:
                searchPathDirectory = .cachesDirectory
            }
    
            if let url = FileManager.default.urls(for: searchPathDirectory, in: .userDomainMask).first {
                return url
            } else {
                fatalError("Could not create URL for specified directory!")
            }
    
        }
    
    }