【问题标题】:Swift 3 error: [_SwiftValue pointSize] unrecognized selector sent to instanceSwift 3 错误:[_SwiftValue pointSize] 无法识别的选择器发送到实例
【发布时间】:2017-01-23 07:39:26
【问题描述】:

我刚刚将我们的项目迁移到 swift 3 并看到很多由于一个问题而崩溃:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[_SwiftValue pointSize]:无法识别的选择器已发送到实例

该错误的原因是调用:

[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:]

我注意到,如果我将 String 转换为 NSString 并在其上调用 boundingRectWithSize,它会抛出该错误。它似乎也发生在许多其他部分,例如,如果我在情节提要中发送视图控制器标题,它会引发相同的错误。

有人遇到同样的问题吗?

重现问题:

在 Xcode 8 中创建一个新的 Swift 3 项目,并在 viewDidLoad 中添加以下行:

let attributes: [String: AnyObject?] = [
            NSFontAttributeName: UIFont.systemFont(ofSize: 14)
        ]
    let boundingRect = ("hello" as NSString).boundingRect(with: CGSize(width: 100, height: 100), options: .usesLineFragmentOrigin, attributes: attributes, context: nil)

但正如我所说,它在许多其他地方都崩溃了,因为 UIKit 似乎在许多部分内部都使用了这种方法

【问题讨论】:

  • 请显示您的代码导致相关部分出现问题。
  • 貌似和NSString的内部实现有关
  • 我遇到了同样的崩溃,但为此:(textLabel.text!as NSString).size(attributes: fontAttributes) 我已经尝试了一切,从使用 nsmutablestring,向它附加字符串等等。仍然崩溃。毫无疑问,这是苹果的错。真的真的很糟糕。无法迁移。
  • 类似的崩溃。苹果在做什么! (不,我不是强行解开“做”;))

标签: ios swift string nsstring uifont


【解决方案1】:

如果我使用你的测试代码,但让attributes的数据类型默认,它不会崩溃。那就是:

let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]

按住 Option 键点击变量表示它是 [String : UIFont]

一点额外的测试,表明它与可选对象有关; [String: AnyObject] 似乎工作正常。

编辑: 毕竟,我决定阅读文档,上面写着要使用[String: Any]。 :)

【讨论】:

  • 很好,菲利普。这里有一点,如果有人使用自定义字体,那么字体应该被解包:UIFont(name: UIFont.lightFontName(), size: 14)!
  • 正确,因为UIFont(name: size:) 返回一个UIFont?。您需要在创建字典条目之前打开它...但最好不要使用 !(除非需要崩溃)。
  • 有一个非常相似的问题,但有颜色。 NSForegroundColorAttributeName 被设置为可选的 NSColor(是的 NS-,在 macOS 上)。 3.0 之前的版本会出错。对一些新的可选处理不太满意。
  • 我在 someLabel.sizeToFit() 中遇到了同样的错误。正如我们在我和 Leonid 的案例中看到的那样,我们想要得到字符串的 rect。因此我们可以假设 Apple 希望我们始终使用未包装的字体来进行计算。
【解决方案2】:

以下为我修复了它:

let attributes: [String: UIFont] = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]

【讨论】:

    【解决方案3】:
    func attributedString(firstText : String, amount : String, fontSize : CGFloat, color : UIColor) -> NSAttributedString {
    
        let attrDict = [ NSFontAttributeName : UIFont(name: fontRegular, size: CGFloat(fontSize/2))!,
                        NSForegroundColorAttributeName : UIColor.darkGray] as [String : AnyObject]
        let iconString = NSMutableAttributedString(string: firstText, attributes: attrDict)
    
        let attrDict1 = [ NSFontAttributeName : UIFont(name: fontRegular, size: CGFloat(fontSize))!,
                         NSForegroundColorAttributeName : color] as [String : AnyObject]
        let amountString = NSMutableAttributedString(string: amount, attributes: attrDict1)
    
        iconString.append(amountString)
        return iconString
    }
    

    然后这样称呼它

    lblBalanceAmount.attributedText = self.attributedString(firstText: "My Balance", amount: "500", fontSize: newFontSize, color: UIColor(red: 41/255.0, green: 192/255.0, blue: 42/255.0,阿尔法:1.0))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-31
      • 2019-01-09
      • 2017-02-14
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多