Reputation: 6804
Took me an hour to figure this iOS crash out, posting the solution in case it helps.
You might have a different value other than count
after NSConcreteNotification
in your crash.
I was crashing on accessing an array.count, which is why it's count in my case:
@objc fileprivate func loadParts(constraints: [NSLayoutConstraint]? = nil) {
assert(Thread.current.isMainThread)
var constraints = constraints ?? [NSLayoutConstraint]()
...
let cCount = constraints.count
I could not for the life of me see how it could crash on constraints.count as the array is guaranteed to exist.
Upvotes: 2
Views: 909
Reputation: 6804
I was wiring this function up to a Notification like this:
NotificationCenter.default.addObserver(self, selector: #selector(self.loadParts), name: UIDevice.batteryStateDidChangeNotification, object: nil)
If you look at the documentation for addObserver, it says the function must have exactly one parameter which is a Notification. What was happening was that my function was being called with a Notification, but my code expected it to be an array.
The fix was to create a new function that simply called the function I wanted (loadParts), and have the Notification hit that instead:
NotificationCenter.default.addObserver(self, selector: #selector(self.loadPartsNotification(_:)), name: UIDevice.batteryStateDidChangeNotification, object: nil)
...
@objc fileprivate func loadPartsNotification(_ notification: Notification) {
self.loadParts()
}
fileprivate func loadParts(constraints: [NSLayoutConstraint]? = nil) {
...
Upvotes: 9