Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handleInput() takes in an object of class AnyObject but should take in an object of class Any in order to support URLs #40

Open
naterock101 opened this issue Feb 2, 2023 · 0 comments

Comments

@naterock101
Copy link

naterock101 commented Feb 2, 2023

In the AttachmentManager class

this method:

open func handleInput(of object: AnyObject) -> Bool {
        let attachment: Attachment
        if let image = object as? UIImage {
            attachment = .image(image)
        } else if let url = object as? URL {
            attachment = .url(url)
        } else if let data = object as? Data {
            attachment = .data(data)
        } else {
            return false
        }
        
        insertAttachment(attachment, at: attachments.count)
        return true
    }

the problem with this is if you try and input a URL from a video such as:

`@objc func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    if let editedImage = info[.editedImage] as? UIImage {
        inputPlugins.forEach { _ = $0.handleInput(of: editedImage) }
    } else if let originImage = info[.originalImage] as? UIImage {
        inputPlugins.forEach { _ = $0.handleInput(of: originImage) }
    } else if let videoURL = info[.mediaURL] as? URL {
        inputPlugins.forEach  { _ = $0.handleInput(of: videoURL) }
    }
    getRootViewController()?.presentedViewController?.dismiss(animated: true, completion: nil)
    inputAccessoryView?.isHidden = false
}

`

you will get an error: "Argument type 'URL' expected to be an instance of a class or class-constrained type"

It seems that the issue lies in the definition of the method handleInput. The method is expecting an argument of type AnyObject, but URL is not a subclass of AnyObject, it's a struct. To resolve the issue, you need to change the definition of handleInput to accept a value of type Any instead:

so that method should look like this:

open func handleInput(of object: Any) -> Bool {
    let attachment: Attachment
    if let image = object as? UIImage {
        attachment = .image(image)
    } else if let url = object as? URL {
        attachment = .url(url)
    } else if let data = object as? Data {
        attachment = .data(data)
    } else {
        return false
    }
    
    insertAttachment(attachment, at: attachments.count)
    return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant