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

0

How do I fix 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)
    
asked by anonymous 28.09.2017 / 16:54

1 answer

3

The problem is that the text property of your field is optional type String? and Swift is an inferred type language. So the type of your sentence object will also be String? . For this to happen, you simply 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"
    
29.09.2017 / 00:55