Why can the keyword “weak” only be applied to class and class-bound protocol types

后端 未结 11 1330
清酒与你
清酒与你 2020-12-13 03:14

When I\'m declaring variables as weak in Swift, I sometimes get the error message from Xcode:

\'weak\' may only be applied to class and

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 04:08

    protocol PenguinDelegate: class {
        func userDidTapThePenguin()
    }
    
    class MyViewController: UIViewController {
        weak var delegate: PenguinDelegate?
    }
    

    If you type class after your protocol it works as well and seems more appropriate that for NSObjectProtocol.

提交回复
热议问题