Swift 3 error: [_SwiftValue pointSize] unrecognized selector sent to instance

不羁的心 提交于 2019-12-05 08:46:10

问题


I just migrated our project to swift 3 and see lots of crashes because of one issue:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue pointSize]: unrecognized selector sent to instance

The reason for that error is the call to:

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

What I noticed is that if I cast String to NSString and call boundingRectWithSize on it it will throw that error. It also seems to be happening in many other parts, for example if I sent a view controller title in a storyboard it throws the same error.

Anyone having the same problems?

To reproduce the problem:

Create a new Swift 3 project in Xcode 8 and add the following line in 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)

But as I said it crashes in many other places as it seems that UIKit uses this method internally in many parts


回答1:


If I use your test code, but let the data type of attributes default, it doesn't crash. That is:

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

Option-clicking on the variable says it's [String : UIFont].

A little extra testing, suggests that it's related to the optional object; [String: AnyObject] appears to work OK.

EDIT: And after all that, I decided to read the documentation, which says to use [String: Any]. :)




回答2:


The following fixed it for me:

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



回答3:


Replacing NSDictionary with [String: Any] will fix the problem. let attributes: [String: Any] = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]




回答4:


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
}

And call it like

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, alpha: 1.0))



来源:https://stackoverflow.com/questions/39508384/swift-3-error-swiftvalue-pointsize-unrecognized-selector-sent-to-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!