Geek Answers Handbook

Adding Methods Dynamically

I am trying to find this method in the link to runj_cpp_cppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

I want to add a new method, for example:

- [AClass drawWithFrame:(NSRect)rect inView:(id)view]

So far I have written a C function:

void drawWithFrameInView(id this, SEL this_cmd, NSRect frame, id view){
...
} 

Now I am ready to do:

BOOL success = class_addMethod(NSClassFromString(@"AClass"), 
                               @selector(drawWithFrame:inView:), 
                               (IMP)drawWithFrameInView, 
                               "v@:@:@:");

but it successnever will be YES, I tried the same approach with methods with simpler signatures, and it worked. Therefore, I think the problem is the last parameter:"v@:@:@:"

What should I pass in this case for my new method to work?

+5
objective-c objective-c-runtime cocoa
nacho4d Jul 25 '11 at 5:16
source share
1 answer

This will work:

char *types = [[NSString stringWithFormat:@"v@:%s@", @encode(NSRect)] UTF8String];

BOOL success = class_addMethod(NSClassFromString(@"MyClass"), 
                               @selector(drawWithFrame:inView:), 
                               (IMP)drawWithFrameInView, 
                               types);

, , , NSRect , typedef - struct.

.

+8
Jacob Relkin 25 . '11 5:24

More articles:

  • Creating a single SELECT using a multi-level parent> child array - arrays
  • YII: How to change the date and time format displayed in a view - php
  • How to register a destructor for a numpy array allocated by C? - c
  • TinyMCE, IE9,

Geek Answers | 2019