You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 3.3迁移至4.1后出现NSFontAttributeName未识别错误求解决

修复Swift 4.1中「Use of unresolved identifier 'NSFontAttributeName'」错误

嘿,这个问题我在从Swift 3.x迁移到4.x版本的时候也踩过坑!原因很明确:Swift 4开始,苹果对NSAttributedString的属性名做了类型安全的重构,原来的字符串常量(比如NSFontAttributeName)被替换成了NSAttributedString.Key枚举里的对应值。

具体修复步骤:

直接把代码里的NSFontAttributeName替换成NSAttributedString.Key.font即可,修复后的完整代码如下:

progressView.titleLabelAttributedText = NSAttributedString(
    string: MyxStrings.sharedInstance.SENDING,
    attributes: [NSAttributedString.Key.font: Scully.sharedInstance.APP_REGULAR_FONT_SMALL!]
)

额外补充(举一反三):

不止字体属性,其他常用的富文本属性也都做了类似的改名,比如:

  • 原NSForegroundColorAttributeName → NSAttributedString.Key.foregroundColor
  • 原NSUnderlineStyleAttributeName → NSAttributedString.Key.underlineStyle
    这种改动是为了让代码更类型安全,避免因拼写错误导致的隐性问题。

内容的提问来源于stack exchange,提问作者podgradle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:52:41