(cls, selector, method, encoding, replace=False)
return imp
Add a new instance method to the given class.

    To add a class method, add an instance method to the metaclass.

    :param cls: The Objective-C class to which to add the method, as an :class:`ObjCClass` or :class:`Class`.
    :param selector: The name for the new method, as a :class:`str`, :class:`bytes`, or :class:`SEL`.
    :param method: The method implementation, as a Python callable or a C function address.
    :param encoding: The method's signature (return type and argument types) as a :class:`list`.
        The types of the implicit ``self`` and ``_cmd`` parameters must be included in the signature.
    :param replace: If the class already implements a method with the given name, replaces the current implementation
        if ``True``. Raises a :class:`ValueError` error otherwise.
    :return: The ctypes C function pointer object that was created for the method's implementation.
        This return value can be ignored. (In version 0.4.0 and older, callers were required to manually
        keep a reference to this function pointer object to ensure that it isn't garbage-collected.
        Rubicon now does this automatically.)
Add a new instance method to the given class.
Add a new instance method to the given class .
def add_method(cls, selector, method, encoding, replace=False):
    """Add a new instance method to the given class.

    To add a class method, add an instance method to the metaclass.

    :param cls: The Objective-C class to which to add the method, as an :class:`ObjCClass` or :class:`Class`.
    :param selector: The name for the new method, as a :class:`str`, :class:`bytes`, or :class:`SEL`.
    :param method: The method implementation, as a Python callable or a C function address.
    :param encoding: The method's signature (return type and argument types) as a :class:`list`.
        The types of the implicit ``self`` and ``_cmd`` parameters must be included in the signature.
    :param replace: If the class already implements a method with the given name, replaces the current implementation
        if ``True``. Raises a :class:`ValueError` error otherwise.
    :return: The ctypes C function pointer object that was created for the method's implementation.
        This return value can be ignored. (In version 0.4.0 and older, callers were required to manually
        keep a reference to this function pointer object to ensure that it isn't garbage-collected.
        Rubicon now does this automatically.)
    """

    signature = [ctype_for_type(tp) for tp in encoding]
    assert signature[1] == objc_id  # ensure id self typecode
    assert signature[2] == SEL  # ensure SEL cmd typecode
    if signature[0] is not None and issubclass(signature[0], (Structure, Union)):
        # Patch struct/union return types to make them work in callbacks.
        # See the source code of the ctypes_patch module for details.
        ctypes_patch.make_callback_returnable(signature[0])
    selector = SEL(selector)
    types = b"".join(encoding_for_ctype(ctype) for ctype in signature)

    cfunctype = CFUNCTYPE(*signature)
    imp = cfunctype(method)
    if replace:
        libobjc.class_replaceMethod(cls, selector, cast(imp, IMP), types)
    else:
        res = libobjc.class_addMethod(cls, selector, cast(imp, IMP), types)

        if not res:
            raise ValueError("A method with the name {!r} already exists".format(selector.name))

    _keep_alive_imps.append(imp)
    return imp
def add_method ( cls , selector , method , encoding , replace = False ) : signature = [ ctype_for_type ( tp ) for tp in encoding ] assert signature [ 1 ] == objc_id # ensure id self typecode assert signature [ 2 ] == SEL # ensure SEL cmd typecode if signature [ 0 ] is not None and issubclass ( signature [ 0 ] , ( Structure , Union ) ) : # Patch struct/union return types to make them work in callbacks. # See the source code of the ctypes_patch module for details. ctypes_patch . make_callback_returnable ( signature [ 0 ] ) selector = SEL ( selector ) types = b"" . join ( encoding_for_ctype ( ctype ) for ctype in signature ) cfunctype = CFUNCTYPE ( * signature ) imp = cfunctype ( method ) if replace : libobjc . class_replaceMethod ( cls , selector , cast ( imp , IMP ) , types ) else : res = libobjc . class_addMethod ( cls , selector , cast ( imp , IMP ) , types ) if not res : raise ValueError ( "A method with the name {!r} already exists" . format ( selector . name ) ) _keep_alive_imps . append ( imp ) return imp