Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

little question about class versioning

1 view
Skip to first unread message

Sebastian Reitenbach

unread,
Sep 13, 2012, 5:52:59 AM9/13/12
to discuss...@gnu.org
Hi,

I have the following test program:

#import <Foundation/Foundation.h>

@interface MyClass: NSObject
@end

@interface MySubClass: MyClass
@end

@implementation MyClass
+(void)initialize
{
[self setVersion: 1];
}
@end

@implementation MySubClass
+(void)initialize
{
[self setVersion: 8];
}

-(id) init
{
self = [super init];
NSLog(@"SuperClass class_getVersion([super class]): %i", class_getVersion([super class]));
NSLog(@"SuperClass class_getVersion([MyClass class]): %i", class_getVersion([MyClass class]));
NSLog(@"SuperClass [[super class] version]: %i", [[super class] version]);
NSLog(@"SubClass class_getVersion([self class]): %i", class_getVersion([self class]));
NSLog(@"SubClass class_getVersion([MySubClass class]): %i", class_getVersion([MySubClass class]));
NSLog(@"SubClass [[self class] version]: %i", [[self class] version]);
return self;
}
@end


int
main(int argc, const char *argv[])
{
id pool = [[NSAutoreleasePool alloc] init];

MySubClass *my = [[MySubClass alloc] init];


[pool release];

return 0;
}

which gives me the following output:

2012-09-13 11:47:30.072 TestTool[27335] SuperClass class_getVersion([super class]): 8
2012-09-13 11:47:30.081 TestTool[27335] SuperClass class_getVersion([MyClass class]): 1
2012-09-13 11:47:30.081 TestTool[27335] SuperClass [[super class] version]: 8
2012-09-13 11:47:30.081 TestTool[27335] SubClass class_getVersion([self class]): 8
2012-09-13 11:47:30.081 TestTool[27335] SubClass class_getVersion([MySubClass class]): 8
2012-09-13 11:47:30.081 TestTool[27335] SubClass [[self class] version]: 8

What I wonder is why line 1 and line 3 returns 8 instead of 1.
I guess there must be a simple solution, but I was looking into NSObject.m but did not found
yet, why I only get the 1 in the 2nd line.

If somebody could explain that, or has a pointer to documentation where to read up,
that would be helpful.

thanks,
Sebastian

Wolfgang Lux

unread,
Sep 13, 2012, 7:28:36 AM9/13/12
to Sebastian Reitenbach, discuss...@gnu.org
Hi Sebastian,

> which gives me the following output:
>
> 2012-09-13 11:47:30.072 TestTool[27335] SuperClass class_getVersion([super class]): 8
> 2012-09-13 11:47:30.081 TestTool[27335] SuperClass class_getVersion([MyClass class]): 1
> 2012-09-13 11:47:30.081 TestTool[27335] SuperClass [[super class] version]: 8
> 2012-09-13 11:47:30.081 TestTool[27335] SubClass class_getVersion([self class]): 8
> 2012-09-13 11:47:30.081 TestTool[27335] SubClass class_getVersion([MySubClass class]): 8
> 2012-09-13 11:47:30.081 TestTool[27335] SubClass [[self class] version]: 8
>
> What I wonder is why line 1 and line 3 returns 8 instead of 1.
> I guess there must be a simple solution, but I was looking into NSObject.m but did not found
> yet, why I only get the 1 in the 2nd line.
>
> If somebody could explain that, or has a pointer to documentation where to read up,
> that would be helpful.

I guess you wonder because you misunderstood what [super class] does.
This method call invokes the class method on the super class of the receiver, i.e., on the super class of SubClass. However, the class method is implemented in NSObject, which itself is a super class of SuperClass. So calling [super class] is just the same as calling [self class] and will return the class of the receiver (and not the receiver's super class as you seem to expect).

Wolfgang


Sebastian Reitenbach

unread,
Sep 14, 2012, 12:44:40 PM9/14/12
to Wolfgang Lux, discuss...@gnu.org
Hi Wolfgang,
Thanks for the explanation. It's still not logical to me, and what I'd expect, but if it is how it is, then I'll have to take it that way.

cheers,
Sebastian

>
> Wolfgang
>






0 new messages