Created
April 8, 2018 02:19
-
-
Save mitulmanish/d1e9395678e9fabb89f4a8410835e9ac to your computer and use it in GitHub Desktop.
This file contains 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
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