假设某些项目可以出现在 Feed
中,只要它们实现了 Feedable
协议定义的必要属性。也让我们说 Photo
对象值得出现在Feed中:
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
或许这个代码片段(尽管不能编译)更能清晰地表达意图。
extension Array where Element: Feedable { ... }
的翻译是:当元素为Feedable时,对数组进行扩展 { ... }
。 - Leo DabusPhoto
对象数组提供一个特定的Feedable
协议实现,而不是其他Feedable
类型的数组。例如,与Video
对象数组不同,Photos
数组可能提供不同的feedDescription
。 - Ben PackardFeedable
协议将为Photo
和一组Photo
提供不同的feedDescription
实现。例如,“添加了一张照片”与“添加了5张照片”。假设我只提供了适合于Feed的项目(照片、视频或其中任何一组)。 - Ben Packardextension _ArrayType where Generator.Element == Photo { ... }
@BenPackardextension _ArrayType where Generator.Element == 照片 { ... }
- Leo Dabus