Reputation: 2500
I have class tructure like this
@interface SuperClass: NSObject
+ (void) someName;
@end
@interface MyClass: SuperClass
@end
There is the case that i only want to call the someName
if it is a class method of MyClass
not MyClass
's superclass. Since [[MyClass class] respondsToSelector:@selector(someName)]
return YES if a class or its super response to the selector. How to tell that MyClass doesnt contain tosomeName
?
In my application i want to print the string that contains chains of string return from a class method.
Take abve class structure as a example, i want to print something like:
somenameFromSuper.somenameFromClass.someNameFromeSubClass.
if a class doesnot implement someName
method, i want to replace it by `notavailable, for ex:
somenameFromSuper.notavailable.someNameFromeSubClass.
Upvotes: 1
Views: 106
Reputation: 16650
_Bool class_implementsMethodForSelector( Class cls, SEL selector )
{
unsigned methodsCount;
Method* methods = class_copyMethodList(cls, &methodsCount);
for (unsigned methodIndex=0; methodIndex<methodsCount; methodIndex++)
{
if (method_getName(methods[methodIndex]) == selector)
{
break;
}
}
free(methods);
return methodsIndex<methodsCount;
}
…
Class classToTest = …;
classToTest = object_getClass(classToTest); // For checking class methods
if (class_implementsMethodForSelector(classToTest, @selector(someName))
{
…
}
else
{
…
}
Typed in Safari.
Edit: Made a function of it. Still typed in Safari.
Upvotes: 4