iOS 10.3 UISegmentedControl setTitleTextAttributes 崩溃

iOS 10.3 UISegmentedControl setTitleTextAttributes Crash

iOS 10.3 在 UISegmentedControlsetTitleTextAttributes 方法上崩溃。

问题:

  1. 崩溃的原因是什么?
  2. 解决方法是什么?

注意:已将此问题报告给苹果,但尚未收到他们的消息。 https://openradar.appspot.com/31448227

示例代码:

class ViewController: UIViewController {

    @IBOutlet private weak var segmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()         
        //Crash!        
        segmentedControl.setTitleTextAttributes([UIFont.systemFont(ofSize: 14.0) : NSFontAttributeName], for: .normal)
    }
}

编辑

正如@vedian 指出的那样,它应该是 [key : value] 而不是相反,但是 上面的代码不会在低于 10.3iOS 的版本上崩溃 .

使用

    segmentedControl.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)], for: .normal)

而不是

segmentedControl.setTitleTextAttributes([UIFont.systemFont(ofSize: 14.0) : NSFontAttributeName], for: .normal)

问题是你造成的,而不是 Apple。

创建字典的顺序是先key然后value

segmentedControl.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 14.0)], for: .normal)