TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

"Unrecognized selector sent to instance" error.

Forums > SwiftUI

Hello all, I'm posting here again to ask for your help on an issue I’m having trouble fixing on my own. In order to make my app more usable on MacOS (it runs on iOS and on Mac Silicon), I tried adding keyboard and menubar commands by following a guide I found elsewhere online (since apparently the topic is not tackled here on HWS). Apparently it seems to work: the new commands are listed in the location I specified, but when I click the menu items or press the associated shortcuts to invoke the newly created commands, the app crashes with the following error: “-[_UIEditMenuInteractionMenuController propertyList]: unrecognized selector sent to instance 0x600003307900”. Below is the relevant code (I apologise in advance for the fact that it will not be formatted as you are used to: I use VoiceOver and the message editor here doesn’t allow me to navigate the text very well, so I’m pasting the message from an external TextEdit file; I also use single spaces instead of tabs for indentation, because I find it easier to understand this way. I hope reading my code isn’t too challenging). // App struct @main struct ColorMatchApp: App { @State var showingAddingView = false @State var showingAddingMultipleView = false var body: some Scene { WindowGroup { ContentView(showingAddingView: $showingAddingView, showingAddingMultipleView: $showingAddingMultipleView) } .commands { CommandGroup(replacing: CommandGroupPlacement.newItem) { Button("New clothing item“) { showingAddingView = true } .keyboardShortcut("n") } CommandGroup(before: CommandGroupPlacement.saveItem) { Button(“Add multiple clothing items“) { showingAddingMultipleView = true } .keyboardShortcut("p") } } } }

// ContentView struct ContentView: View { @Binding var showingAddingView: Bool @Binding var showingAddingMultipleView: Bool var body: some View { NavigationStack { // ... .toolbar { Button(“Add”) { showingAddingView = true } .highPriorityGesture( LongPressGesture().onEnded { _ in showingAddingMultipleView = true }) } } .sheet(isPresented: $showingAddingView) { ClothingEditorView(showing: $showingAddingView) } .sheet(isPresented: $showingAddingMultipleView) { AddingMultipleView(showing: $showingAddingMultipleView) } } }

I tried adding an exception breakpoint to investigate the issue, but Xcode just points to the “struct ColorMatchApp” line. Also, replacing the body of the two commands with simple print statements doesn’t resolve the problem. Any help would be greatly appreciated, and thanks for taking the time to read my post.

3      

My best attempt at a formatted version of the original code:

// App struct
@main 
struct ColorMatchApp: App { 
  @State var showingAddingView = false 
  @State var showingAddingMultipleView = false 

  var body: some Scene { 
    WindowGroup { 
      ContentView(showingAddingView: $showingAddingView, showingAddingMultipleView: $showingAddingMultipleView) 
    } 
    .commands { 
      CommandGroup(replacing: CommandGroupPlacement.newItem) { 
        Button("New clothing item“) { 
          showingAddingView = true 
        } 
        .keyboardShortcut("n") 
        } 
      CommandGroup(before: CommandGroupPlacement.saveItem) { 
        Button(“Add multiple clothing items“) { 
          showingAddingMultipleView = true 
        } 
        .keyboardShortcut("p") 
      }
    } 
  } 
}

// ContentView 
struct ContentView: View { 
  @Binding var showingAddingView: Bool 
  @Binding var showingAddingMultipleView: Bool 

  var body: some View { 
    NavigationStack { 
      // ... 
    }
    .toolbar { 
      Button(“Add”) { 
        showingAddingView = true 
      } 
      .highPriorityGesture( LongPressGesture().onEnded { _ in 
        showingAddingMultipleView = true 
      }) 
    } 
  } 
    .sheet(isPresented: $showingAddingView) { 
      ClothingEditorView(showing: $showingAddingView) 
    } 
    .sheet(isPresented: $showingAddingMultipleView) { 
      AddingMultipleView(showing: $showingAddingMultipleView) 
    } 
  } 
}

Are you using MacCatalyst for the Mac version? What guide did you use? You might not be able to link to it because of anti-spam measures on the forum, but at least provide the name of the guide.

You may also want to use the command key modifier for the menu keyboard shortcuts so Cmd-N creates the new item instead of pressing N.

.keyboardShortcut("n", modifiers: [.command])

2      

@SwiftDevJournal, Thanks for your reply. I am using the “My Mac (designed for iPad)” destination in Xcode to run the app on MacOS, which means it runs only on Macs that have the M1 chip. I am not sure if this is Mac Catalyst or something different. The guide I followed to create the commands is called “Commands in SwiftUI”, frm the site “Swift with Majid”. Thanks for the tip about using the command modifier, but apparently the keyboardShortcut modifier already defaults to that key. This is confirmed by the documentation provided by Apple, where the implementation of the modifier is like this: func keyboardShortcut( _ key: KeyEquivalent, modifiers: EventModifiers = .command ) -> some View So the shortcuts already require the command key along with n or p. Some more research shows that this might actually be a bug on Apple’s part that causes a crash if there is a keyboard shortcut associated with the menu command: I found a post on the Apple Developer Forums where someone described exactly the same issue. The post is titled “Menu works fine in iPad and Mac Catalyst but crashed on Apple Silicon”. In fact, removing the keyboardShortcut modifier makes the menu items work as expected (except that now they can no longer be invoked using the keyboard). I guess I will stick to this compromise until this issue is fixed, unless someone could give me a suggestion on how to define a shortcut for the hightPriorityGesture (creating one for the button should be easy enough). Thanks again for your help.

2      

I have not tried to build a Mac version of an iPad app so I can’t give you a solution to your specific problem. But I can share what I learned when I was unable to show a sheet from a menu in a Mac SwiftUI app. Maybe it will help you solve your issue.

When an app shows a sheet, the sheet appears in front of a window. If you try to show a sheet directly from a menu, the sheet won’t appear because the menu doesn’t have a window. You have to get a window from which you can attach a sheet.

To get the window so you can attach the sheet, you must add a focused value for showing a sheet and set the focused scene value in the content view. I wrote an article with more details and code samples. Unfortunately I can't link to it here. If you add .com to my screen name here, you can reach my website. The main page of the site has a list of SwiftUI articles. The article Showing a SwiftUI sheet from a Mac Menu is on the list and is the article that has the details and code samples.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.