예제 #1
0
func (cls Class) AddMethod(methodName string, implementor interface{}) bool {

	v := reflect.ValueOf(implementor)

	if (v.Kind() == reflect.Func) && (v.Type().NumIn() > 1) {

		types := "v"
		impName := trimPackage(runtime.FuncForPC(v.Pointer()).Name())
		numArgs := v.Type().NumIn()

		if v.Type().NumOut() == 1 {
			types = objcArgTypeString(trimPackage(v.Type().Out(0).String()))
		}

		for i := 0; i < numArgs; i++ {
			argType := trimPackage(v.Type().In(i).String())
			types = types + objcArgTypeString(argType)

		}

		sel := C.sel_registerName(C.CString(methodName))
		imp := loadThySelf(impName)
		result := C.class_addMethod(cls.cclass(), sel, imp, C.CString(types))

		return (result == 1)
	}

	return false
}
예제 #2
0
func Runtime_class_addMethod(cls unsafe.Pointer, sel unsafe.Pointer, imp unsafe.Pointer, types string) bool {
	var n *C.char = C.CString(types)
	defer C.free(unsafe.Pointer(n))
	if C.class_addMethod((*C.struct_objc_class)(cls), (*C.struct_objc_selector)(sel), (*[0]byte)(imp), n) == 1 {
		return true
	} else {
		return false
	}
}