我目前正在尝试为 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/