Skip to content

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

Description

@naterock101

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions