What are the differences between this protocol definition?

Lately, I got a little confused about defining methods for a fast protocol.

protocol OneDelegate : class

or

protocol OneDelegate : NSObjectProtocol

or

@objc protocol OneDelegate

or

@class_protocol protocol OneDelegate

And will we use the weak for the delegate? Or how  unowned(unsafe) var dataSource: UITableViewDataSource?

Thanks for the help!

+4
source share
1 answer

The first and last are actually the same. Both indicate that the protocol can only be accepted by the class, i.e. The structure cannot accept the protocol. Form protocol OneDelegate : classis preferred. And @class_protocol protocol OneDelegateout of date.

The second case is a way to extend an existing protocol. Say, for example, you want your UITableViewDelegateanswer to a long press, then you can define the protocol:

protocol UITableViewDelegateWithRecognizer: UITableViewDelegate {
    func longPressed()
}

UITableViewDelegateWithRecognizer UITableViewDelegate longPressed.

@objc, , Objective-C. , , @objc.

, ARC Swift Objective-C, weak. , Swift , . . , , , , - , .. .

+4

Source: https://habr.com/ru/post/1606894/


All Articles