swift xcode7

swift - ''UIFont'' no es convertible a ''UIFont?''



xcode download (3)

En otro lugar en SO, alguien sugiere que donde tienes esto:

aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)

... deberías intentar escribir esto:

aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)

No puedo atribuirme el mérito de esto (porque no puedo reproducir el error ) ¡así que estoy adivinando! Pero sería muy interesante saber si realmente funciona.

Así que actualicé mi XCode a 7.3 hoy por la noche.

En uno de mis proyectos, aparece el siguiente error en algunas etiquetas donde establezco la fuente:

''(name: String, size: CGFloat) -> UIFont'' is not convertible to ''(name: String, size: CGFloat) -> UIFont?''

EDITAR: Este es mi código para la Vista de título en la barra de navegación:

let aTitleFrame: CGRect = CGRectMake(0, aHeaderTitleSubtitleView.frame.midY / 2, 200, 24) let aTitleView: UILabel = UILabel(frame: aTitleFrame) aTitleView.backgroundColor = UIColor.clearColor() aTitleView.font = UIFont(name: "Roboto-Regular", size: 15) // ERROR POPS UP HERE aTitleView.textAlignment = NSTextAlignment.Center aTitleView.textColor = UIColor.whiteColor()

Este es mi código para una Cadena Atribuida para un UILabel:

let aAttributedFundLabel: NSMutableAttributedString = NSMutableAttributedString(string: "Raising/n$ /(fund)") aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSRange(location: 0, length: 7)) aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 15)!, range: NSRange(location: 0, length: 7)) // ERROR POPS UP HERE aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSRange(location: 8, length: fund.characters.count + 2)) aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 16)!, range: NSRange(location: 8, length: fund.characters.count + 2)) // ERROR POPS UP HERE startupFund.attributedText = aAttributedFundLabel

Esto sucede solo en dos archivos en todo mi proyecto.

Abrí otro proyecto, pero pude construirlo y ejecutarlo sin ningún error, a pesar de que también establecí la fuente para varias etiquetas allí.

¿Alguna idea de por qué está pasando esto?

TIA!


Tuve el mismo problema y hacer esto me permite volver a compilar:

let descriptor = UIFontDescriptor(name: "OpenSans-Semibold", size: 10.0) label.font = UIFont(descriptor: descriptor, size: 10.0)

Entonces, usa el UIFontDescriptor ...

También hacer esto me funciona:

if let font = UIFont(name: "OpenSans-Semibold", size: 10) { label.font = font }


Yo tuve este problema también. Lo que me solucionó fue desactivar la optimización de todo el módulo.

Configuración de Bulid> Compilador de Swift - Generación de código> Nivel de optimización

Lo configuré con la opción Más rápida (optimización de todo el módulo). Cuando lo configuré en None ya no tenía este problema. Por contexto, esta es una base de código mixto con Objective-C y Swift.