Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Failed to load method of Objective-C protocol" even when the protocol method is not being implemented #1702

Closed
brianquinlan opened this issue Nov 5, 2024 · 0 comments · Fixed by #1705

Comments

@brianquinlan
Copy link
Contributor

This can happen if the method is not supported by the current OS version.

ffigened implement/implementAsListener calls objc.getProtocolMethodSignature even for functions that are not installed.

e.g.

abstract final class NSStreamDelegate {
  /// Builds an object that implements the NSStreamDelegate protocol. To implement
  /// multiple protocols, use [addToBuilder] or [objc.ObjCProtocolBuilder] directly.
  static objc.ObjCObjectBase implement(
      {void Function(NSStream, NSStreamEvent)? stream_handleEvent_}) {
    final builder = objc.ObjCProtocolBuilder();
    NSStreamDelegate.stream_handleEvent_
        .implement(builder, stream_handleEvent_);  // `NSStreamDelegate.stream_handleEvent_` will be loaded even if `stream_handleEvent_` is `null`
    return builder.build();
  }
}

I think that there are two things to do here:

  1. Don't call getProtocolMethodSignature when the signature isn't needed
  2. getProtocolMethodSignature should provide better diagnostics e.g.
objc.NSMethodSignature getProtocolMethodSignature(
  Pointer<c.ObjCProtocol> protocol,
  Pointer<c.ObjCSelector> sel, {
  required bool isRequired,
  required bool isInstanceMethod,
}) {
  final sig =
      c.getMethodDescription(protocol, sel, isRequired, isInstanceMethod).types;
  if (sig == nullptr) {
    throw SelectorNotSupportedException(
        'Failed to load method of Objective-C protocol: ${protocol.toDartString()} ${sel.toDartString()}');
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants