-3

I encountered some kind of a problem while using a Swift Playground and trying to set up a simple Delegate Design.

The problem seems to be the fact, that protocols can not be marked as public but the source folder in a Playground is considered as a new Module. Therefore I can't find a solution for this problem.

Here is my code so far (IN THE SOURCE FOLDER, THE VIEWCONTROLLER IS IN THE PLAYGROUND FILE)

//MARK: - Imports
import UIKit

//MARK: - Protocols
protocol UIManagerDelegate {
    func didChangePage(forward: Bool)
}

//MARK: - Properties & Initialisers
public class UIManager {

    // Properties
    private let parentView : UIView
    private var delegate: UIManagerDelegate

    // Initialisers
    public init(for view: UIView, delegate: UIManagerDelegate) {

        self.parentView = view
        self.delegate = delegate
    }
}

The error message I get is the following: Initializer cannot be declared public because its parameter uses an internal type. And when trying to mark the protocol as public, this also produces an error.

Do you guys have any idea on how to fix this issue? Thanks a lot for your help in advance.

christophriepe
  • 1,157
  • 12
  • 47
  • Unrelated to your original question, but we generally define our protocols to be `class` protocols (e.g. `protocol UIManagerDelegate: class { ... }`) and make the `delegate` property a `weak` reference (e.g. `private weak var delegate: UIManagerDelegate?`). Strong references to delegate objects is a really easy was to introduce strong reference cycles. – Rob May 10 '20 at 17:40
  • 1
    I’d also recommend against using `UI` prefix as that is intended for UIKit symbols and your use of it is likely to be a source of confusion in the future. – Rob May 10 '20 at 17:43

1 Answers1

2

make the delegate public

public protocol UIManagerDelegate
ugur
  • 3,604
  • 3
  • 26
  • 57