假设某些项目可以出现在Feed
中,只要它们实现由Feedable
协议定义的必要属性即可。让我们也说,Photo
对象是饲料值得:使Swift阵列符合协议
extension Photo: Feedable { }
是否可以说,这些照片的Array
也可能是Feedable
?
extension [Photo] : Feedable
或者,我总是需要某种形式的包装对象的,如PhotoAlbum
,以符合Feedable
?
编辑
再次重申,我很好奇,我是否可以Photo
对象Feedable
的唯一阵列。没有制作Array
任何类型的内容Feedable
,而不是制作Feedable
本身的Feedable
(如果这是您所需要的,它们都是以下解决方案提供的)。
换句话说,一个溶液(我怀疑存在)将允许我限定Feedable
类型的变量具有下列的结果:
var feedable: Feedable
//photo is feedable, so this is fine
feedable = Photo() //ok
//arrays of photos are feedable
let photo1 = Photo()
let photo2 = Photo()
feedable = [photo1, photo2]
//arrays of other things are not
feedable = ["no", "dice"] //nope
//even if the contents of an array are themselves Feedable, that's not sufficient. E.g. Video is Feedable, but Array of Videos is not.
let video1 = Video()
let video2 = Video()
feeble = video1 //fine
feedable = [video1, video2] //nope
也许this gist(其不编译当然)示出了意图更清楚。
'扩展数组元素:可馈入{...}' –
@LeoDabus我期待为'Photo'对象数组和其他'可供给类型数组'提供'可馈入'协议的特定实现。 。例如,一组照片可能提供一个'feedDescription',它与'Video'对象的数组不同。 –
@matt'可馈入'协议将为'照片'和一组'照片'提供不同的'feedDescription'实现。例如。 “添加了一张照片”vs“添加了5张照片”。假设我只提供了饲料价值物品(照片,视频或任何一组)。 –