Skip to content

Instantly share code, notes, and snippets.

@Daemon-Devarshi
Created October 26, 2019 05:30
Show Gist options
  • Save Daemon-Devarshi/45590c543fbfd1e02dd106336880f38d to your computer and use it in GitHub Desktop.
Save Daemon-Devarshi/45590c543fbfd1e02dd106336880f38d to your computer and use it in GitHub Desktop.
A protocol and its extension supporting serialization of Plist to respective data model
import Foundation
/// Protocol supporting decoding of a plist to associated data model
protocol PlistDecoder {
associatedtype PlistModel: Decodable
func decodedValue() -> PlistModel?
}
/// Implementation of PlistDecoder with decoding capability
extension PlistDecoder {
func decodedValue() -> PlistModel? {
if let path = Bundle.main.path(forResource: String(describing: PlistModel.self), ofType: "plist"), let xml = FileManager.default.contents(atPath: path) {
do {
return try PropertyListDecoder().decode(PlistModel.self, from: xml)
}
catch {
print(error)
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment