[英]Cannot convert value of type '[String : Any]' to expected argument type 'String'
[英]Cannot convert value of type 'UIImage?' to expected argument type '[Any]'
我正在尝试使用共享按钮发送图像
我在UIViewcontroller中有一个名为self.image的图像
func share(_ sender: Any) {
let imageToShare = self.image?
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
但是我收到以下错误
Cannot convert value of type 'UIImage?' to expected argument type '[Any]'
可能这与self.image的可选值类型有关吗? 当有人单击按钮时函数不会崩溃时,如何使其变为非可选?
activityItems
期望获得一个数组。 因此,您可以使imageToShare
为非可选,并将其作为数组发送(仅包含此图像)。
if let
成为非可选 [imageToShare]
一个包含此图像的数组 完整的解决方案是:
if let imageToShare = self.image? {
let activityViewController = UIActivityViewController(activityItems: [imageToShare], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
如果您要理解的错误是:
无法转换“ UIImage”类型的值? 到预期的参数类型“ [Any]”
从字面上讲,该类型(是否为可选!)不是预期的[Any]
类型。 由于UIImage
可以是Any
,那么您如何尝试let imageToShare = [self.image]
?
在这种情况下,您将输入[Any?]
。 Any?
Array
对象。 现在,如果它仍然抱怨(可能会)您已经知道的可选事物(然后可能会),然后以您想要的任何方式安全地将其包装,以使其成为[Any]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.