Creating a Swift Array Complies with the Protocol

Let's say that some elements may appear in Feed if they implement the necessary properties defined by the Feedable protocol. Let them also say that the Photo object is worthy of submission:

 extension Photo: Feedable { } 

Is it possible to say that the Array these photos can also be Feedable ?

 extension [Photo] : Feedable 

Or do I always need some kind of wrapper object like PhotoAlbum to fit Feedable ?

Edit

To re-iterate, I was curious if I could only create arrays of Photo Feedable objects. Do not create an Array for any type of Feedable content without creating the Feedable array by Feedable itself (both of which are offered as solutions below, if necessary).

In other words, a solution (which I doubt exists) would allow me to define a variable of type Feedable with the following results:

 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 not sufficient. Eg Video is Feedable, but Array of Videos is not. let video1 = Video() let video2 = Video() feeble = video1 //fine feedable = [video1, video2] //nope 

Perhaps this meaning (which, of course, does not compile) shows intent more clearly.

+9
arrays ios swift protocols
source share
4 answers

If there was an array of Photo and Video , who would you like to be?

1. Each element does the same as they.

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

2. The entire array works as a "Video".

 extension Array where Element : Photo { func foo() { } } 
+2
source share

I think this is currently not possible. In my project, I have the same problem with 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() } } 

I am using ModelType as a ghost protocol. The problem is that I cannot create a model manufacturer that produces several ModelType s for the same reason that you discovered. The solution in this case was as follows:

 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()] } } 

It is more flexible from the start. I am getting rid of an optional variable, and manufacturers of one model have only one element in the array. Perhaps you can use a similar approach.

+1
source share

You can achieve your goal as follows:

Swift 4:

 protocol Feedable { func foo() } extension String: Feedable { func foo() { } } extension Array: Feedable where Element: Feedable { func foo() { } } 
0
source share

I did not try to play in the playground, but maybe you can just create an array with the ability to feed:

 var myPhotosArray = [Feedable]() 

Then everything that implements the Feedable protocol will be resolved in the array. If you only need an array of photos, you can still subclass your Photo object to create a FeedablePhoto object.

Try this on the Playground instead of downvoting without even checking. Seriously 3 downvotes without any reason or explanation ...

 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'" 
-4
source share

All Articles