符合协议要求委托变量在 ios13 中可用

发布于 2025-01-13 10:02:10 字数 1577 浏览 3 评论 0原文

拥有这个协议

 public protocol URLSessionWebSocketTaskProtocol {

    func send(_ message: URLSessionWebSocketTask.Message, completionHandler: @escaping (Error?) -> Void)
    func receive(completionHandler: @escaping (Result<URLSessionWebSocketTask.Message, Error>) -> Void)
    func sendPing(pongReceiveHandler: @escaping (Error?) -> Void)
    func cancel(with closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
    func resume()
}

并在这部分遵守它

extension URLSessionWebSocketTask: URLSessionWebSocketTaskProtocol {}

可以很好地工作,但是我需要在协议中拥有委托属性,因此将协议修改为这个

public protocol URLSessionWebSocketTaskProtocol {

func send(_ message: URLSessionWebSocketTask.Message, completionHandler: @escaping (Error?) -> Void)
func receive(completionHandler: @escaping (Result<URLSessionWebSocketTask.Message, Error>) -> Void)
func sendPing(pongReceiveHandler: @escaping (Error?) -> Void)
func cancel(with closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
func resume()
var delegate: URLSessionTaskDelegate? { get set}

}

现在编译器告诉

协议“URLSessionWebSocketTaskProtocol”要求“委托”在 iOS 13.0.0 及更高版本中可用

目标最低版本设置为 iOS 13,因此应该有必要添加 @available(iOS 13.0, *),但添加任何方式和编译器一直告诉我们协议需要委托在 iOS 13 中可用,但没有发生任何变化。 URLWebSocketTask 本身没有委托属性,它继承自 URLSessionTask,后者具有 URLSessionTaskDelegate 类型的委托属性,并且 URLWebSocketTaskDelegate 符合 URlSessionTaskDelegate。

那么我如何向协议添加委托并且一致性不会显示该错误。

having this protocol

 public protocol URLSessionWebSocketTaskProtocol {

    func send(_ message: URLSessionWebSocketTask.Message, completionHandler: @escaping (Error?) -> Void)
    func receive(completionHandler: @escaping (Result<URLSessionWebSocketTask.Message, Error>) -> Void)
    func sendPing(pongReceiveHandler: @escaping (Error?) -> Void)
    func cancel(with closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
    func resume()
}

and conforming to it in this part

extension URLSessionWebSocketTask: URLSessionWebSocketTaskProtocol {}

works fine, but I need to have delegate property in the protocol so modified the protocol to this

public protocol URLSessionWebSocketTaskProtocol {

func send(_ message: URLSessionWebSocketTask.Message, completionHandler: @escaping (Error?) -> Void)
func receive(completionHandler: @escaping (Result<URLSessionWebSocketTask.Message, Error>) -> Void)
func sendPing(pongReceiveHandler: @escaping (Error?) -> Void)
func cancel(with closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)
func resume()
var delegate: URLSessionTaskDelegate? { get set}

}

now the compiler tells that

Protocol 'URLSessionWebSocketTaskProtocol' requires 'delegate' to be available in iOS 13.0.0 and newer

the target minimun version is set to iOS 13, so it should be necessary to add the @available(iOS 13.0, *), but added any way and there is not change the compilers keeps telling that protocol requires delegate to be available in iOS 13.
URLWebSocketTask does not have a delegate property per se it inherits from URLSessionTask that have a delegate Property of type URLSessionTaskDelegate and URLWebSocketTaskDelegate comforms to URlSessionTaskDelegate.

so how I could add a delegate to the protocol and the conformance does not show that error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2025-01-20 10:02:10

URLSessionWebSocketTask 是一个 NSURLSessionTask,自 iOS 15 以来仅具有 delegate(!!):

@interface NSURLSessionTask : NSObject <NSCopying, NSProgressReporting>

@property (nullable, retain) id <NSURLSessionTaskDelegate> delegate 
API_AVAILABLE(macos(12.0), ios(15.0), watchos(8.0), tvos(15.0));

因此,如果您想符合该 API 约定,您需要在协议中执行相同的操作,例如:

public protocol URLSessionWebSocketTaskProtocol {

    // ... other methods here

    @available(iOS 15, *)                               // << here !!
    var delegate: URLSessionTaskDelegate? { get set }

}

当然,替代方案是将扩展限制为 iOS15+,例如

@available(iOS 15, *)
extension URLSessionWebSocketTask: URLSessionWebSocketTaskProtocol {
}

使用 Xcode 13.2.1 进行验证

The URLSessionWebSocketTask is-a NSURLSessionTask, which has delegate only (!!) since iOS 15:

@interface NSURLSessionTask : NSObject <NSCopying, NSProgressReporting>

@property (nullable, retain) id <NSURLSessionTaskDelegate> delegate 
API_AVAILABLE(macos(12.0), ios(15.0), watchos(8.0), tvos(15.0));

so if you want to fit that API contract, you need to do same in your protocol, like:

public protocol URLSessionWebSocketTaskProtocol {

    // ... other methods here

    @available(iOS 15, *)                               // << here !!
    var delegate: URLSessionTaskDelegate? { get set }

}

of course alternate is to make extension limited to iOS15+, like

@available(iOS 15, *)
extension URLSessionWebSocketTask: URLSessionWebSocketTaskProtocol {
}

Verified with Xcode 13.2.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文