Type cast from a method in SEL

Im using the Objective-C runtime library, the class_copyMethodList () function, to get a list of all the methods in my class. How then to convert type type objects to SEL objects using SEL type?

+4
source share
3 answers

Run the method_getName() function on the return objects of the method.

+5
source

I did this a couple of years ago to extract all the class method names. You can use NSSelectorFromString () to get SEL from each name.

 + (NSArray *) methodNamesForClass:(Class) aClass { Method *methods; unsigned int methodCount; if (methods = class_copyMethodList(aClass, &methodCount)) { NSMutableArray *results = [NSMutableArray arrayWithCapacity:methodCount]; while (methodCount--) [results addObject:[NSString stringWithCString: sel_getName(method_getName(methods[methodCount])) encoding: NSASCIIStringEncoding]]; free(methods); return results; } return nil; } 
+3
source

Assuming you have a method name, you can convert it to a selector using the NSSelectorFromString function.

 SEL fooSelector = NSSelectorFromString ( @"foo:" ) ; 

Apple discusses this in its Objective-C tutorial .

-2
source

Source: https://habr.com/ru/post/1380097/


All Articles