UIAlertController crash in Catalyst/UIKitForMac

I have a problem with using UIAlertController in my apps built for UIKitForMac: If I press the "OK" button in the box displaying the message, the app crashes with an "-[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array"exception like this:


2019-06-09 18:47:35.613479+0200 macalerts[3018:179110] [General] *** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array

2019-06-09 18:47:35.613566+0200 macalerts[3018:179110] [General] (

0 CoreFoundation 0x00007fff3a56fc63 __exceptionPreprocess + 250

1 libobjc.A.dylib 0x00007fff6fa3806b objc_exception_throw + 48

2 CoreFoundation 0x00007fff3a62f12a _CFThrowFormattedException + 202

3 CoreFoundation 0x00007fff3a504f4a -[__NSArrayM objectAtIndexedSubscript:] + 142

4 UIKitCore 0x00007fff768389d9 __48-[UIAlertController(iOSMac) _createBridgedAlert]_block_invoke_2 + 75

5 libdispatch.dylib 0x00000001003849b3 _dispatch_call_block_and_release + 12

6 libdispatch.dylib 0x000000010038598f _dispatch_client_callout + 8

7 libdispatch.dylib 0x000000010039583a _dispatch_main_queue_callback_4CF + 1865

8 CoreFoundation 0x00007fff3a4becaa __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

9 CoreFoundation 0x00007fff3a4be3e8 __CFRunLoopRun + 2370

10 CoreFoundation 0x00007fff3a4bd821 CFRunLoopRunSpecific + 499

11 HIToolbox 0x00007fff3919d7fd RunCurrentEventLoopInMode + 292

12 HIToolbox 0x00007fff3919d53d ReceiveNextEventCommon + 600

13 HIToolbox 0x00007fff3919d2c7 _BlockUntilNextEventMatchingListInModeWithFilter + 64

14 AppKit 0x00007fff3784ccf8 _DPSNextEvent + 990

15 AppKit 0x00007fff3784ba6b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352

16 AppKit 0x00007fff378461b7 -[NSApplication run] + 658

17 AppKit 0x00007fff3783804b NSApplicationMain + 777

18 AppKit 0x00007fff37cbc022 _NSApplicationMainWithInfoDictionary + 16

19 UIKitMacHelper 0x00007fff6a599ef9 UINSApplicationMain + 424

20 UIKitCore 0x00007fff765bea91 UIApplicationMain + 2206

21 macalerts 0x0000000100002b6b main + 75

22 libdyld.dylib 0x00007fff70d967a9 start + 1

)


This even happens when I "isolate" the problem in a simple single view app where I bring up a UIAlertController in viewDidAppear like this:


override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

let ac = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

self.present(ac, animated: true)

}


Does anybody know ho to solve this?

Accepted Reply

Hi. This was posted a while ago but I thought I'd respond in case anyone else has the same problem and winds up here.

In Catalyst a UIAlertViewController with no UIAlertActions still shows an 'OK' button. Clicking that button leads to the crash.

In order to fix this you must add your own empty 'OK' UIAlertAction to the controller before presenting it.

Replies

Hi. This was posted a while ago but I thought I'd respond in case anyone else has the same problem and winds up here.

In Catalyst a UIAlertViewController with no UIAlertActions still shows an 'OK' button. Clicking that button leads to the crash.

In order to fix this you must add your own empty 'OK' UIAlertAction to the controller before presenting it.

Thanks - yes, exactly this is the problem here, the crash appears only for alerts with no actions.

I use them for progress messages which disappear automatically if the work is completed.


Adding a 'dummy' action fixes the crash, but to make the alert disappear you have to press this button.

Calling [UIViewController dismissViewControllerAnimated:completion:] executes the completion code,

but does not hide the alert.


Is there a solution without this dummy button?


Is it possible to use the NSAlert from AppKit directly here in an UIKitForMac app?