Error "Type does not conform to Protocol"

I have reduced my error to the following code and am stumped trying to resolve... help would be appreciated. xItem does conform to SCListItem which is the protocol requirement for SCList ... so why am I getting the error?

protocol SCList {
    var items: [ any SCListItem ] { get set }
}

protocol SCListItem {
}

class xItem: SCListItem {
}

class xItemList: SCList { // ERROR Type 'xItemList' does not conform to protocol 'SCList'
    var items: [ xItem ] = []
}
Answered by ForumsContributor in

You have declared a get / set in protocol, you need to do the same in the class.

protocol SCList {
    var items: [ any SCListItem ] { get set }
}

protocol SCListItem {
}

class xItem: SCListItem {
}

class xItemList: SCList {
    var items: [SCListItem]  { // }= []
        get {
            return []
        }
        set(newValue) {
            // ... do whatever is appropriate ...
        }
    }
}

read interesting discussion here: https://stackoverflow.com/questions/40820913/how-to-conform-to-a-protocols-variables-set-get

Accepted Answer

The error message occurs because your SCList protocol requires an array of items of existential type (any SCListItem), but your actual array declaration has items of a concrete type (xItem), which is non-matching type.

To understand why, think about what would happen if you had another SCListItem class as well:

class yItem: SCListItem {
}

According to the SCList protocol conformance, your xItemList array should be able to store values of any type that conforms to SCListItem, including both xItem and yItem — that's what an existential type means. But your actual xItemLIst only supports an array of items of the exact type xItem, which isn't what the protocol conformance promises.

Error "Type does not conform to Protocol"
 
 
Q