objc2 icon indicating copy to clipboard operation
objc2 copied to clipboard

Failed overriding protocol method -[NSWindowDelegate canBecomeKeyWindow]: method not found

Open superman2211 opened this issue 2 months ago • 2 comments

Hi! I have a problem with debug build my application.

If i build app in debug mode i have an error in runtime:

thread 'main' panicked at /Users/username/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/objc2-0.6.2/src/__macro_helpers/define_class.rs:328:21:
failed overriding protocol method -[NSWindowDelegate canBecomeKeyWindow]: method not found

Error here:

        #[cfg(debug_assertions)]
        if let Some(protocol) = self.protocol {
            let _types = self
                .required_instance_methods
                .iter()
                .chain(&self.optional_instance_methods)
                .find(|desc| desc.sel == sel)
                .map(|desc| desc.types)
                .unwrap_or_else(|| {
                    panic!(
                        "failed overriding protocol method -[{protocol} {sel}]: method not found"
                    )
                });
        }

My code:

pub struct WindowState {}

define_class!(
	#[unsafe(super = NSWindow)]
	#[ivars = WindowState]
	#[thread_kind = MainThreadOnly]
	pub struct Window;

	unsafe impl NSObjectProtocol for Window {}

	unsafe impl NSWindowDelegate for Window {
		#[unsafe(method(canBecomeKeyWindow))]
		fn can_become_key_window(&self) -> bool {
			true
		}
	}
);

Am I doing something wrong?

In release mode all works fine.

System info:

Macbook Pro (Apple M3 Pro) macOS Tahoe 26.0.1 rustc 1.89.0 (29483883e 2025-08-04) cargo 1.89.0 (c24e10642 2025-06-23)

Libs:

objc2 = "0.6.1"
objc2-foundation = { version = "0.3.1", features = [
    "NSEnumerator",
    "NSThread",
] }
objc2-app-kit = { version = "0.3.1", features = [
    "NSApplication",
    "NSEvent",
    "NSGraphicsContext",
    "NSResponder",
    "NSRunningApplication",
    "NSView",
    "NSWindow",
    "NSWindowScripting",
    "NSTrackingArea",
    "NSCursor",
] }
objc2-core-foundation = { version = "0.3.1" }
objc2-core-graphics = { version = "0.3.1" }
dispatch2 = { version = "0.3.0" }

superman2211 avatar Oct 17 '25 08:10 superman2211

Yeah, that check is wrong, I plan to fix it in objc2 v0.7.0.

This is also reported in https://github.com/madsmtm/objc2/issues/645.

Use the following in the meantime:

# In top-level Cargo.toml
[profile.dev.package.objc2]
debug-assertions = false

madsmtm avatar Oct 17 '25 09:10 madsmtm

Thank you! All works!

superman2211 avatar Oct 17 '25 09:10 superman2211