Skip to content

Instantly share code, notes, and snippets.

@piercifani
Created February 26, 2015 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piercifani/23640a8211a0e891c4a5 to your computer and use it in GitHub Desktop.
Save piercifani/23640a8211a0e891c4a5 to your computer and use it in GitHub Desktop.
Generics and Protocols issue
//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