ios - 如何为 URLSessionDataTask 分配委托(delegate)?

标签 ios swift delegates

我目前正在尝试为 URI 创建一个 URLSessionDataTask。因为 URI 是重定向的,所以我需要找出重定向的(最终)URL 是什么。为此,我想在 URLSessionDataDelegate 上使用 willPerformHTTPRedirection 方法(此方法继承自其父类(super class) URLSessionDelegate)

问题是:我无法在 URLSessionDataTask 上找到要将委托(delegate)分配给的属性。

根据我的理解,如果一个类(例如,class A)有一个委托(delegate)类(例如,class ADelegate),那么 中也应该有一个属性>class A 通常以delegate 后缀命名。当我们想要实现委托(delegate)的方法时,我们实例化委托(delegate)类的实例,并将其分配给委托(delegate)属性。例如:

var instanceA = A()
var instanceADelegate = ADelegate()

instanceA.delegate = instanceADelegate

我的理解有误吗?在 URLSessionDataDelegate 中实现方法然后在 URLSessionDataTask 中使用它的正确方法是什么?

最佳答案

The problem is: I could not find a property on URLSessionDataTask to assign the delegate to

因为委托(delegate)属于 URLSession。来自 the docs :

protocol URLSessionDataDelegate The NSURLSessionDataDelegate protocol defines the methods that a delegate of an URLSession object can implement to handle task-level events specific to data tasks and upload tasks.

(强调我的。)

关于ios - 如何为 URLSessionDataTask 分配委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46046205/

相关文章:

ios - Swift 中的基于文本的游戏

c++ - iOS集成unity 4.6.3项目(64位&& IL2CPP)。出现如下这样的错误,如何解决呢?

ios - 如何在 iOS 中沿圆圈移动 View Controller

iphone - 为 UIImagePicker 提供您自己的 UIImage。

ios - 动画委托(delegate)未转换为 Swift 3.0

ios - swift iOS : EXC_BAD_INSTRUCTION

swift - 当 bool 值为 true 时如何删除该节点?

iphone - 在外部库协议(protocol)中添加和使用新方法时出现警告

C#:用于创建委托(delegate)列表的不那么难看的语法?

iphone - iOS 中的委托(delegate)