Skip to content

Instantly share code, notes, and snippets.

@mitulmanish
Created April 8, 2018 02:19
Show Gist options
  • Save mitulmanish/d1e9395678e9fabb89f4a8410835e9ac to your computer and use it in GitHub Desktop.
Save mitulmanish/d1e9395678e9fabb89f4a8410835e9ac to your computer and use it in GitHub Desktop.
protocol ViewModelProtocol: class {
associatedtype Item: Equatable
init?()
var items: [Item] { get }
var count: Int { get }
func getData(forResourceType resource: ResourceType) throws -> Data
func extractItems(fromData data: Data) -> [Item]
var resourceType: ResourceType { get }
}
extension ViewModelProtocol {
var count: Int {
return items.count
}
func getData(forResourceType resource: ResourceType) throws -> Data {
var data: Data!
do {
data = try resource.load()
} catch let err {
throw err
}
return data
}
func extractItems(fromData data: Data) -> [Item] {
let items = try? JSONDecoder().decode([Item].self, from: data)
return items ?? []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment