Created
January 27, 2016 20:39
-
-
Save Makazone/339730b8b3d85fd7bad2 to your computer and use it in GitHub Desktop.
Protocol inheritance in Swift
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import UIKit | |
protocol A { } | |
protocol B: A { } | |
class MyClass: B { } | |
let b: [B] = (1...3).map { MyClass(indx: $0) } | |
// Cannot convert value of type '[B]' to specified type '[A]' | |
let a: [A] = b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution: let a: [A] = b.map { $0 as A }
Explanation: http://stackoverflow.com/questions/33090626/swift-upcasting-array-of-protocol-to-array-of-super-protocol-causes-error