【发布时间】:2016-03-31 02:59:00
【问题描述】:
假设某些项目可以出现在Feed 中,只要它们实现了Feedable 协议定义的必要属性。我们还假设Photo 对象是值得提要的:
extension Photo: Feedable { }
是否可以说这些照片中的Array 也可能是Feedable?
extension [Photo] : Feedable
或者我是否总是需要某种包装对象,例如PhotoAlbum,以符合Feedable?
编辑
再次重复,我很好奇我是否只能制作Photo 对象Feedable 的数组。不制作任何内容类型的 Array Feedable,不制作 Feedables 本身的数组 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(当然不会编译)更清楚地表明了意图。
【问题讨论】:
-
extension Array where Element: Feedable { ... } -
@LeoDabus 我希望为
Photo对象数组与其他Feedable类型数组提供Feedable协议的特定实现。例如,照片数组可能会提供一个feedDescription,它不同于Video对象数组。 -
@matt
Feedable协议将为Photo与一组Photos 提供不同的feedDescription实现。例如。 “添加了一张照片”与“添加了 5 张照片”。假设我只获得了值得供稿的项目(一张照片、一段视频或其中的一组)。 -
@BenPackard
extension _ArrayType where Generator.Element == Photo { ... } -
@matt 在我看来,这与不输入所有数组
[AnyObject]的原因相同 - 只是认为只有照片、视频和其他处理类型符合的数组会很好我可以扔掉 Feedable 类型的对象,因为我知道它们总是能够给我合理的提要描述。
标签: ios arrays swift protocols