代码之家  ›  专栏  ›  技术社区  ›  Leonid

Swift 3错误:[_SwiftValue pointSize]无法识别发送到实例的选择器

  •  32
  • Leonid  · 技术社区  · 9 年前

    由于未捕获的异常“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)
    

    3 回复  |  直到 9 年前
        1
  •  31
  •   Phillip Mills    9 年前

    如果我使用您的测试代码,但让 attributes 默认情况下,它不会崩溃。即:

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

    选项单击变量表示 [String : UIFont] .

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

    编辑: 在所有这些之后,我决定阅读文档,其中说要使用 [String: Any]

        2
  •  1
  •   Hamza Azad    9 年前

    let attributes: [String: UIFont] = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]
    
        3
  •  0
  •   Ga Ne Sh    9 年前
    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
    }
    

    并像这样称呼它

    推荐文章