Artem Stepanenko Artem Stepanenko - 5 months ago 14
Objective-C Question

Non-virtual methods in Objective-C

I need to call from the category a concrete implementation of the method (even if it's overridden). Is it possible? (Just in case: I know, all methods in Objective-C are virtual.)

// class A
@interface A

- (void)foo;

@end

@implementation A

- (void)foo
{
NSLog(@"initial foo");
}

@end

// subclass of A
@interface A1: A

@end

@implementation A1

- (void)foo
{
NSLog(@"overridden foo");
}

@end

// category to A
@interface A (Category)

- (void)bar;

@end

@impletemtation A (Category)

- (void)bar
{
[self foo]; // probably, I should specify here what exactly must be self
// but I don't know how
}

@end

/////////////////////////////////////////////////////////////////
A1 *a = [[A1 alloc] init];

[a bar]; // now the "overridden foo" string will be printed
// my goal is to print "initial foo" !!!

Answer

Of course, you do not have to change anything.

You can get a function pointer to the implementation of every method on a specific class:

A* receiver = …
IMP impOnA = class_getMethodImplementation( [A class], @selector( foo ));
impOnA( receiver, @selector( foo ) );