Object does not respond to selector

I'm trying to add functionality to save a picture from a URL to the user's photo library. here's the code:

import Foundation
import UIKit

class ImageDownloader: NSObject {
    static func writeToPhotoAlbum(imageUrl: String) {
        let imageURL = URL(string: imageUrl)!

        if let image = try? Data(contentsOf: imageURL) {
            UIImageWriteToSavedPhotosAlbum(UIImage(data: image)!, self, nil, nil)
        }
    }

    @objc func saveCompleted(
        _ image: UIImage,
        didFinishSavingWithError error: Error?,
        contextInfo: UnsafeRawPointer)
    {
        print("Save finished!")
    }

}

When i call the method like this:

ImageDownloader.writeToPhotoAlbum(imageUrl: "image_url")

I get this error:

ImageDownloader does not respond to selector

Any explanation and solution?

It's a little bit late to reply but I think the error stems from trying to access the self inside a static func. Instead of declaring writeToPhotoAlbum as a static, declare it without static keyword and call it after creating an instance of ImageDownloader.

Like:

import Foundation
import UIKit

class ImageDownloader: NSObject {
    func writeToPhotoAlbum(imageUrl: String) {
        // ...
    }

    @objc func saveCompleted(
        _ image: UIImage,
        didFinishSavingWithError error: Error?,
        contextInfo: UnsafeRawPointer)
    {
        // ...
    }

}

Then,

var imageDownloader: ImageDownloader?
imageDownloader = ImageDownloader()
imageDownloader?.writeToPhotoAlbum(imageUrl: imgUrl)

These may not be directly compilable but it may give a basic idea about the problem.

Object does not respond to selector
 
 
Q