I get the error described under when trying to change color on the text inside an UIAlertController
:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x60000003e320'
Function that tries to change the color:
@objc private func submitScore(color: Bool = false) {
var mess = ""
mess = color ? "Invalid username" : ""
let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")
//Submit button calls function again
let submit = UIAlertAction(title: "Submit", style: .default) { (submit) in
self.submitScore(color: true)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (cancel) in
})
alertController.addTextField(configurationHandler: {(textfield) in
textfield.placeholder = "Nickname"
})
alertController.addAction(submit)
alertController.addAction(cancel)
present(alertController, animated: true, completion: {(alertController) in
print("shown")
})
}
The problem is solved if i remove line 5. NSForegroundColorAttributeName
is a valid NSAttributedString-property so I don't understand the error..