2016-03-31 70 views
8

假设某些项目可以出现在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(其不编译当然)示出了意图更清楚。

+2

'扩展数组元素:可馈入{...}' –

+0

@LeoDabus我期待为'Photo'对象数组和其他'可供给类型数组'提供'可馈入'协议的特定实现。 。例如,一组照片可能提供一个'feedDescription',它与'Video'对象的数组不同。 –

+0

@matt'可馈入'协议将为'照片'和一组'照片'提供不同的'feedDescription'实现。例如。 “添加了一张照片”vs“添加了5张照片”。假设我只提供了饲料价值物品(照片,视频或任何一组)。 –

回答

-4

我没在操场上尝试,但也许你可以简单地馈送的数组:

var myPhotosArray = [Feedable]() 

然后实现可送布协议都将在阵列允许的。如果你只想要一个照片数组,你仍然可以继承你的Photo对象来创建一个FeedablePhoto对象。

在Playground中尝试此操作,而不是在没有测试的情况下进行downvoting。 认真3 downvotes没有任何理由和解释......

import UIKit 

protocol Tree: class { 
    func grow() 
} 

class BigTree: Tree { 
    internal func grow() { 
    print("Big tree growing") 
    } 
} 

class SmallTree: Tree { 
    internal func grow() { 
    print("Small tree growing") 
    } 
} 

class Car { 
    //not a tree 
} 

var manyTrees = [Tree]() 

manyTrees.append(BigTree()) 
manyTrees.append(SmallTree()) 
manyTrees.append(Car()) //This makes an error "Car doesn't conform to expected type 'Tree'" 
+0

你甚至在这篇文章上做了-1之前尝试过吗?我试过了,它工作。 – Mikael

+0

我自己并没有-1,但这并没有回答这个问题。我认为这就是为什么它被低估了。 –

+0

从我的理解,它的确如此。只有实现Feedable协议的类可以添加到Array中。问题在于:“是否有可能说这些照片的阵列也可能是可馈送的?”我想我的答案不容易理解,我没有用Swift 3来试试这个,但Swift 2。只要让一个Photo类实现协议,就是这样。 – Mikael

3

如果是的PhotoVideo一个数组,你想什么呢?

1.每个元素的表现都与它们相同。

extension Array where Element : Feedable { 
    func foo() { 
     if Element.self == Photo.self { 

     } else { 

     } 
    } 
} 

2.整个阵列执行'视频'。

extension Array where Element : Photo { 
    func foo() { 

    } 
} 
+0

我想要前者,但不必允许任何类型的所有数组都是'可馈入的'。 –

+0

@BenPackard元素是数组的一个通用类型。带有条件的扩展只有在数组确认了所有条件时才有效。 – Lumialxk

+0

@ ben-packard提出的解决方案中的“where”子句意味着此扩展只适用于阵列中的元素类型符合可馈送协议而不是任何类型的所有阵列的阵列 –

0

我认为这是目前不可能的。在我的项目中,我与ModelProducer有同样的问题。

protocol M: ModelType {} 
protocol ModelProducerType { 
    associatedtype M: ModelType 
    var model: M? { get } 
    func produce() 
} 

struct Test: ModelType {} 
class TestProducer: ModelProducerType { 
    var model: Test? 
    func produce() { 
     model = Test() 
    } 
} 

我使用ModelType作为幻影协议。问题是我不能让一个模型生产者生产多个ModelType s,因为你发现了同样的原因。在这种情况下的解决方案如下:

protocol M: ModelType {} 
protocol ModelProducerType { 
    associatedtype M: ModelType 
    var model: [M] { get } 
    func produce() 
} 

struct Test: ModelType {} 
class TestProducer: ModelProducerType { 
    var model: [Test] = [] 
    func produce() { 
     model = [Test()] 
    } 
} 

这从一开始就更加灵活。我摆脱了可选变量,单个模型生成器只有一个项目在数组中。也许你可以使用类似的方法。