Created
October 26, 2019 05:30
-
-
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
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
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