Created
February 26, 2015 10:34
-
-
Save piercifani/23640a8211a0e891c4a5 to your computer and use it in GitHub Desktop.
Generics and Protocols issue
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
//MARK: Model structs | |
public struct Comic { | |
let id : Int | |
let title : String | |
let thumbnail : Image | |
} | |
public struct Image { | |
let path : String | |
let imageExtension : String | |
public func urlPath () -> String { | |
return "\(path).\(imageExtension)" | |
} | |
} | |
//MARK: Generics functions | |
public class Box<T> { | |
public let value: T | |
public init(_ value: T) { self.value = value } | |
} | |
func sampleComics() -> Box<Comic> { | |
let testImage = Image(path: "http://imgur.com/erflwm1", imageExtension: "gif") | |
let testComic = Comic(id: 0, title: "funny-gif", thumbnail: testImage) | |
return Box(testComic) | |
} | |
//MARK: MVVM stuff | |
protocol ComicViewModel { | |
var displayTitle : String { get } | |
var displayThumbnailPath : String { get } | |
} | |
extension Comic : ComicViewModel { | |
var displayTitle : String { | |
return title | |
} | |
var displayThumbnailPath : String { | |
return thumbnail.urlPath() | |
} | |
} | |
func fetchComicsViewModel() -> Box<ComicViewModel> { | |
return sampleComics() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment