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
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]
. :)