Swift 3错误:[_SwiftValue pointSize]无法识别的选择器发送到实例 [英] Swift 3 error: [_SwiftValue pointSize] unrecognized selector sent to instance
问题描述
我刚刚将项目迁移到swift 3并因为一个问题看到了很多崩溃:
I just migrated our project to swift 3 and see lots of crashes because of one issue:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [_ SwiftValue pointSize]:无法识别的选择器发送到实例
该错误的原因是调用:
[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:]
我注意到的是,如果我将String转换为NSString并在其上调用 boundingRectWithSize
,则会抛出该错误。它似乎也发生在许多其他部分,例如,如果我在故事板中发送了一个视图控制器标题,它会抛出相同的错误。
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?
重现问题:
在Xcode中创建一个新的Swift 3项目8并在viewDidLoad中添加以下行:
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)
但正如我所说,它在其他许多地方崩溃,因为看起来UIKit在许多部分内部使用此方法
But as I said it crashes in many other places as it seems that UIKit uses this method internally in many parts
推荐答案
如果我使用您的测试代码,但让数据类型 attributes
默认情况下,它不会崩溃。那就是:
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键点击该变量表示它是 [String:UIFont]
。
Option-clicking on the variable says it's [String : UIFont]
.
进行一些额外测试,表明它与可选对象有关; [String:AnyObject]
似乎工作正常。
A little extra testing, suggests that it's related to the optional object; [String: AnyObject]
appears to work OK.
编辑:
毕竟,我决定阅读文档,其中说使用 [String:Any]
。 :)
这篇关于Swift 3错误:[_SwiftValue pointSize]无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!