私はDownloaderDelegateプロトコルを使用してDownloaderHandlerと通信するviewControllerを持っています。私のメソッドが呼び出されないのはなぜですか?
私のプロトコルは次のように定義されます、ラインAAを取得します。私はViewControllerを
class ViewController: UIViewController {
weak var downloadHandler : DownloaderHandler?
override func viewDidLoad() {
super.viewDidLoad()
downloadHandler = DownloaderHandler()
downloadHandler?.delegate = self
changeBackground()
}
func changeBackground(){
let googleURL = URL(fileURLWithPath: "https://www.google.com/doodle4google/images/splashes/featured.png")
print(googleURL)
downloadHandler?.downloadData(url:googleURL) // Line BB
}
}
extension ViewController : DownloaderDelegate{
func didReceive(data: Data) {
let image = UIImage(data: data)
let imageView = UIImageView(image: image!)
view.insertSubview(imageView, at: 0)
}
}
を持っていると私のように委任クラスを持っている
protocol DownloaderDelegate : class {
func didReceive(data:Data)
}
:ブレークポイントを使用して
class DownloaderHandler : NSObject, URLSessionDelegate{
weak var delegate :DownloaderDelegate?
var downloadsSession: URLSession = {
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration)
return session // Line AA
}()
func downloadData(url: URL){ // Line CC
downloadsSession.dataTask(with: url){ data, response, error in
print("error is \(error), data is \(data) and response is \(response)") // Line DD
if let badError = error {
print(" the url didn't succeeed error is \(badError.localizedDescription)")
}
else if let someResponse = response as? HTTPURLResponse {
if someResponse.statusCode == 200{
self.delegate?.didReceive(data: data!)
}
}
}
}
}
ロードされる。ラインBBコール。ラインCCは決して呼び出されません。どうして?何が間違っているのですか?
hah私はこの「weak var delegate:DownloaderDelegate? 'それで。 – Honey
私はどこにあなたの考えに間違っていたかを推測しようとしている段落を追加しました::) – matt
私は '弱い 'を削除しました。今私はラインCCをヒットしましたが、私は決して完了DDの中の何も実行されません。完成ハンドラに何か問題はありますか? – Honey