Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created June 15, 2018 10:34
Show Gist options
  • Save onmyway133/a698454108c205b55f3b83018ba75e6b to your computer and use it in GitHub Desktop.
Save onmyway133/a698454108c205b55f3b83018ba75e6b to your computer and use it in GitHub Desktop.
class DataProducer<T: Codable> {
let object: T?
let image: Image?
init(object: T) {
self.object = object
self.image = nil
}
init(image: Image) {
self.image = image
self.object = nil
}
func toData() -> Data {
if let object = object {
let encoder = JSONEncoder()
return try! encoder.encode(object)
} else if let image = image {
return image.cache_toData()!
} else {
return Data()
}
}
}
class MyStorage {
func save<T>(dataProducer: DataProducer<T>, forKey key: String) {
save(data: dataProducer.toData(), forKey: key)
}
func save(data: Data, forKey key: String) {
diskStorage.save(data, forKey: String)
}
}
let storage = MyStorage()
storage.save(dataProducer: DataProducer<String>(object: "hello world"), forKey: "string")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment