Error: Expression implicitly coerced from 'String? ' to Any

Asked

Viewed 243 times

0

How do I correct the following error:

Expression implicitly coerced from 'String? ' to Any

The error is given in the code snippet below:

let frase = lblFrase.text

let textToShare = [ frase ]

///O erro esta nesta linha    
let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)

activityViewController.popoverPresentationController?.sourceView = self.view

activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.mail ]

self.present(activityViewController, animated: true, completion: nil)
  • 1

    do not need to add Uiactivitytype to the elements of excludedActivityTypes. Just pass the cases [ .airDrop, .mail ]

1 answer

5


The problem is that the property text of your field is of optional type String? and Swift is an inferred type language. So the type of your phrase object will also be String?. For this not to happen just use an operator called nil coalescing Operator ?? which gives you the opportunity to choose a default value for your Object if text has no value or if it has not yet been initialized.

Instead of

let frase = lblFrase.text

Use

let frase = lblFrase.text ?? "default value"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.