robdashnash robdashnash - 2 months ago 7x
iOS Question

Are private swift protocols possible?

After reading the docs about this I discovered

In Swift, as in Objective-C, protocol conformance is global—it is not possible for a type to conform to a protocol in two different ways within the same program.

So what is the purpose of the private prefix here

private protocol PartyFormViewControllerDelegate: class {
func partyFormViewController(controller: PartyFormViewController, cancelButtonPressed button: UIBarButtonItem)

class PartyFormViewController: GenericViewController {


In Swift private means: visibile within the current source file

A private Type could conform to a private protocol, look here

private protocol Animal { }
private class Dog: Animal { }

class Zoo {
    private var animals = [Animal]()
    var count : Int { return animals.count }

Here Animal and Dog are visibile only within the current files. However they are used buy Zoo which has internal visibility and exposes the count of the animals to the whole module.