Skip to content

Instantly share code, notes, and snippets.

@SteeweGriffin
Last active February 15, 2018 13:00
Show Gist options
  • Save SteeweGriffin/568ef9fb29d4d8ad66a3644e09d77962 to your computer and use it in GitHub Desktop.
Save SteeweGriffin/568ef9fb29d4d8ad66a3644e09d77962 to your computer and use it in GitHub Desktop.
import UIKit
protocol Printable {
func printMe()
}
class PFObject:Printable {}
class PFDictionary:Printable {
var data:[String:Any] = [:]
}
extension Printable where Self: PFObject {
func printMe() {
print(self)
}
}
extension Printable where Self: PFDictionary {
func printMe() {
print(data["autore"])
}
}
func myGenericFunc<T:Printable>(objs: [T]) {
objs.forEach { (object) in
object.printMe()
}
}
let pfObject = PFObject()
let pfDictionary = PFDictionary()
pfDictionary.data["autore"] = "Pippo Baudo"
let objsA: [PFObject] = [pfObject]
let objsB: [PFDictionary] = [pfDictionary]
myGenericFunc(objs: objsA)
myGenericFunc(objs: objsB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment