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
相关产品推荐
相关产品推荐

