Simon Ross
Simon Ross

Reputation: 1

How to get superclass' instance of object

I'm writing a project and I would like to get the superclass' instance of a certain object at runtime, for development purposes. For example, if I have this method in a class called SomeClass:

+(id)generateSuperclassInstanceOfObject:(id)object {
    if (object) {
        NSLog(@"The class of the object is %@", [object class])
        Class klass = [object superclass];
        if (!klass)
            NSLog(@"Klass not available");
        else {
            //NSLog(@"The superclass of the object is %@", NSStringFromClass(klass));
            id superObject = (klass *)object;
            //NSLog(@"Correctly generated the superclass instance!");
            return superObject;
        }
    }
    return NULL;
}

And, if I have:

@interface A : NSObject
...
@end

@interface B : A
...
@end

@interface C : B
...
@end

I would like to do something similar, if it has at least 2 superclasses (as in my case):

C *object = [[C alloc] init];

id superInstance = [SomeClass generateSuperclassInstanceOfObject:object];
NSLog(@"Generated correct superInstance of class: %@", [superInstance class]);

id superSuperInstance = [SomeClass generateSuperclassInstanceOfObject:superInstance];
NSLog(@"Generated correct superInstance of class: %@", [superSuperInstance class]);

Where NSLog prints:

The class of the object is C
Generated correct superInstance of class: B
The class of the object is B
Generated correct superInstance of class: A

In my code, it looks

id superObject = (klass *)object;

is not correct because klass is not a type.. I need something that allows me to cast an object in this particular situation, using a "cast generated at runtime", because I don't know from the beginning the hierarchy of the object.

The most important part is that: I know "super" exists, but I need to create a new object deleting any reference to its subclass. This must be valid for any object having at least 2 superclasses (so in my case, I shouldn't be able to use the generateSuperclassInstanceOfObject method with argument superSuperInstance, because it has NSObject as superclass).

Any help please?

Upvotes: 0

Views: 200

Answers (2)

trapper
trapper

Reputation: 11993

Casting one object as another doesn't change the underlying object at all. So this doesn't change anything.

id superObject = (klass *)object;

If you want an instance of the super object then you need to alloc/init it like any other object.

id superObject = [[klass alloc] init];

Not sure why you want to do this though... but that is your answer.

Upvotes: 0

CRD
CRD

Reputation: 53000

In general you cannot do what you are trying to do. An object is an instance of a particular class and that never changes.

When an reference to an object of one class is cast to be a reference to an object of a different class the reference object is not changed in anyway, what changes is how the compiler treats the reference.

From your question it is not clear for what purpose you wish to do this. If your intent is to call methods that are inaccessible because they are overridden then the super mechanism exists for that purpose.

If this answer doesn't answer your question you can ask a new question, or edit the current one, detailing the purpose of what you are trying to do, what you have tried, etc. and someone will probably help you along. (I.e. don't try to expand this question in comments.)

HTH

Upvotes: 1

Related Questions