当前位置: 首页 > 面试题库 >

“ UIFont”不能转换为“ UIFont?”

靳睿
2023-03-14
问题内容

因此,我今天晚上将XCode更新为7.3。

在我的一个项目中,在设置字体的几个标签上出现以下错误:

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

编辑: 这是我的导航栏中标题视图的代码:

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()

这是我的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

在我整个项目中,只有两个文件会发生这种情况。

我打开了另一个项目,但是即使在此我也确实为多个标签设置了字体,我也可以构建并运行它而没有任何错误。

知道为什么会这样吗?

TIA!


问题答案:

在SO的其他地方,有人建议您在这里:

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

…您应该尝试编写以下代码:

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

我对此一无所获(因为 我无法重现该错误 ),所以我只是在猜测!但是知道它是否真的会很有趣。



 类似资料:
  • 我刚开始编程,我总是收到错误消息,“不兼容的类型,int不能转换为int[]”,问题是如果R1和R2的长度相等,就把它们加在一起,如果不是,打印一条消息,说“数组必须是相同的长度”,如果这很重要,不确定我在哪里出错了,任何帮助都将非常感谢

  • 我在HackerRank(对角线差异)上做这个问题,我现在面临一个问题。 以下是问题的链接: https://www.hackerrank.com/challenges/diagonal-difference/problem 问题是,我总是得到一个错误:。 我尝试初始化变量j,但这也不能解决问题。 下面是我的代码:

  • 问题内容: 我正在使用Spring来为专门用于使用Hibernate的DAO类的依赖关系进行接线,但是却遇到了一个让我感到困惑的异常: $ Proxy58无法转换为UserDao 我的DAO的配置如下: 我有一个接口,抽象基类和最终实现,如下所示。 接口: 抽象基类: 实现方式: 以下引发上述异常: UserDao dao =(UserDao)context.getBean(“ userDao”)

  • 请帮我把这个移开。下面是我的代码: 这是我从php文件中得到的json

  • 问题内容: 我有。我想使用获得最大结果。这是我的代码: 这是我的: 现在我得到了。怎么了? 问题答案: 您的错误可能在以下行中: 其中query.list()返回BigInteger列表而不是Long列表。尝试将其更改为。