1

iOS 10.3 crashes on UISegmentedControl's setTitleTextAttributes method.

Question:

  1. What is the reason for the crash?
  2. What is the solution?

Note: Have already reported this issue to apple, but haven't yet heard from them. https://openradar.appspot.com/31448227

Sample Code:

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)
    }
}

EDIT

As @vedian pointed out, it should be [key : value] and not the other way around, but the above code doesn't crash on iOS versions below 10.3.

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51

2 Answers2

3

You are causing the issue, not Apple.

A dictionary is created in order first key then value.

segmentedControl.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 14.0)], for: .normal)
vadian
  • 274,689
  • 30
  • 353
  • 361
0

use

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

instead of

segmentedControl.setTitleTextAttributes([UIFont.systemFont(ofSize: 14.0) : NSFontAttributeName], for: .normal)
Sandeep P
  • 94
  • 7