0

He migrado a Swift 4 pero tengo el siguiente error que no consigo resolver:

Cannont convert value of type '[String: AnyObject]' to expected aegument type '[NSAttributedString.DocumentReadingOptionKey: Any]'

let encodedData = label.text!.data(using: String.Encoding.utf8)!
let attributedOptions: [String: AnyObject] = [
    NSAttributedString.DocumentAttributeKey.documentType.rawValue: NSAttributedString.DocumentType.html as AnyObject,
    NSAttributedString.DocumentAttributeKey.characterEncoding.rawValue: String.Encoding.utf8.rawValue as AnyObject
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
label.text = attributedString.string

El error me aparece aqui:

let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

En la variable: attributedOptions

2
  • creo no es necesario colocar el 'try' a menos que tengas un do { "codigo" } y demás, por ejemplo yo tengo en un proyecto lo siguiente para poder asignar los atributos a un texto: let atributeString = [NSAttributedStringKey.font: UIFont(name: "fuente", size: 20)!, NSAttributedStringKey.foregroundColor: UIColor.white].tabBarController?.navigationController?.navigationBar.titleTextAttributes = atributeString. tal vez si colocas un poco mas de codigo ver como lo tienes asignado te podamos ayudar de manera mas sencilla el 4 ene. 2018 a las 16:00
  • al ser un objeto 'AnyObject', puede que al cambiarlo por 'Any' te solucione el problema el 4 ene. 2018 a las 20:53

1 respuesta 1

2

En un Playground esto compila

let attributedOptions: [NSAttributedString.DocumentReadingOptionKey: Any] = [
    .documentType : NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue
]

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.