0
protocol MyProtocol {}

func foo<Item: MyProtocol>(item: Item) {
    //...
}

I have a function foo that takes an item conforming to the MyProtocol protocol. Now I want the function also to accept arrays of items conforming to MyProtocol. Is it possible to make Array<MyProtocol> itself conform to MyProtocol?

KMV
  • 749
  • 1
  • 6
  • 16
  • Technically any type can conform to any protocol if it is needed. But what are you trying to achieve? – Shripada Nov 30 '15 at 10:49

1 Answers1

0

You can make your foo method take an array as a parameter. That should do it.

func foo<Item: MyProtocol>(items: [Item]) {
}
Adam
  • 26,549
  • 8
  • 62
  • 79
  • I am making a two dimensional generic collection view, so cells within cells could be horizontally scrollable. Ideally I would want to reuse everything for both sequences and single instances of a type. In reality `foo` would be something like `func configure(cell: UICollectionViewCell, item: Item)` – KMV Nov 30 '15 at 10:56
  • Then it would look like `func configure(cell: UICollectionViewCell, items: [Item])`. – Adam Nov 30 '15 at 18:59