[英]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.