Nil与预期的参数类型'UIImage'不兼容

时间:2018-06-29 10:31:28

标签: ios swift

这是我的代码,

self.plistModification(urlStr: urlString, uploadedImage:nil )

我可以知道如何将图像值分配为nil。

我的plistModification函数如下所示,每次调用该函数时,uploadImage的值(UIImage)都会更改。

   func plistModification(urlStr : String , uploadedImage : UIImage)
{
    self.activityView.isHidden = false
    SBNetworkManager().getDetails(sortType: "", URLString: urlStr, param: "") { (dictList, error) in


            self.imageUpload(imageData: uploadedImage)

            DispatchQueue.main.async {
                self.activityView.isHidden = true
            }
            if self.flag == 1
            {
                GIDSignIn.sharedInstance().signOut()
            }

        }
        else
        {
            DispatchQueue.main.async {
                self.activityView.isHidden = true
                if self.flag == 1
                {
                    let alert = UIAlertController(title: "Alert", message: dictList!["status_message"]as? String, preferredStyle: .alert)
                    let alertAction = UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                        GIDSignIn.sharedInstance().signOut()
                    })
                    alert.addAction(alertAction)
                    self.present(alert, animated: true, completion: nil)
                }
            }

        }

2 个答案:

答案 0 :(得分:2)

您可以像这样添加空白图像

  self.plistModification(urlStr: urlString, uploadedImage:UIImage() )

或您可以将方法参数设置为可选参数,例如

   func plistModification(urlStr: String, uploadedImage:UIImage?)

在第二个选项中,您可以传递nil值

答案 1 :(得分:1)

    self.plistModification(urlStr: urlString)
//OR

    self.plistModification(urlStr: urlString, uploadedImage:imageData)
    //it may be image or nil


    func plistModification(urlStr: string, uploadedImage:UIImage? = nil){

    }

代替将新的图像对象或nil传递给函数,您可以在函数级别将其声明为默认值的可选参数。因此,当您需要传递nil时,不要在函数调用中写比参数多的参数