I am currently trying to decode Property list using PropertyListEncoder
and Swift 4 Codable
protocol.
I tried some basic types (Strings, Ints, ..) and these all works just fine, but I am not able decode URL. I read multiple articles on this topic and I am pretty sure this should just work. However, the following example fails the decoding with this error:
Expected to decode Dictionary<String, Any> but found a string/data instead.
I think this works correctly with .json
files and I didn't find any informations about different support for codable types in JSONDecoder
and PropertyListDecoder
. Could this be caused by parser incompatibility?
I am using Xcode 9.1 and Swift 4.0.2.
Sample .plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>web</key>
<string>https://link.to</string>
</dict>
</plist>
Sample Swift code:
struct Info: Codable {
let web: URL
}
func loadInfo() {
let propertiesDecoder = PropertyListDecoder()
let data = try! Data(contentsOf:
Bundle.main.url(forResource: "web", withExtension: "plist")!)
try! propertiesDecoder.decode(Info.self, from: data)
}
Thanks for any help!