FileManager extension for PropertyListEncoder

Continuing with SwifterSwift

Image for post
Image for post

After adding two FileManager extension methods to encode/decode objects using JSON encoder, i was asked to make counterparts (encoder and decoder) for PropertyListEncoder.

Both encoders have same methods for encoding & decoding, which means I was able to use the same logic, doing minimum changes to implement new methods.

So I decided to start with an encoder. Just like JSONEncoder, PropertyListEncoder requires an object that conforms to Encodable(or Codable) protocol in order to be able to encode it.

The encode method of the encoder returns and object of type Data which is exactly what FileManager needs in order to create a file using createFile method.

So the code would look something like this:

let data = try encoder.encode(object)

I implemented a generic extension method for FileManager encodePlist as:

public func encodePlist<T: Encodable>(_ object: T, using encoder:PropertyListEncoder=PropertyListEncoder(), to url: URL, attributes: [FileAttributeKey:Any]? = nil) throws {

Unit test for this method would need to test the next:

  • make sure that the extension method does not throw an error
  • check if the file was created at a given url
  • get data back from file
  • decode data to the data type (same data type)
  • check if original data object is the same as decoded object
  • remove the file
func testEncodePlist() {

Initially I implemented encodePlist as encode. But since new method for encoding using PropertyListEncoder had the same signature as the encodemethod with JSONEncoder, I need to make some changes.

I renamed both methods to reflect the encoder name in the name of the function. So for PropertyListEncoder the function was renamed as enodePlist. And the method that I created before was renamed to encodeJSON.

Written by

Computer programming and Analysis student at Seneca College.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store