Created
June 15, 2018 10:34
-
-
Save onmyway133/a698454108c205b55f3b83018ba75e6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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