def add_method(cls_name, selector_name, fn, type_encoding):
cls = ObjCClass(cls_name).ptr
selector = sel(selector_name)
if c.class_getInstanceMethod(cls, selector):
error('Failed to add method, class {} already provides method {}'.format(cls_name, selector_name))
return
parsed_types = parse_types(type_encoding)
restype = parsed_types[0]
argtypes = parsed_types[1]
IMPTYPE = CFUNCTYPE(restype, *argtypes)
imp = IMPTYPE(fn)
retain_global(imp)
did_add = c.class_addMethod(cls, selector, imp, c_char_p(type_encoding.encode('utf-8')))
if not did_add:
error('Failed to add class method')
return did_add
评论列表
文章目录