在实现 NSArray 的子类(类簇)时,我惊讶地发现我的覆盖描述方法没有被调用。有人可以解释这里发生了什么吗?
@interface MyArrayClassCluster : NSArray
@end
@implementation MyArrayClassCluster
{
NSArray *_realArray;
}
// Implement the class cluser stuff here
- (NSUInteger)count
{
return [_realArray count];
}
- (id)objectAtIndex:(NSUInteger)index
{
return [_realArray objectAtIndex:index];
}
// lifeCycle
- (id)initWithItems:(NSArray *)items
{
self = [super init];
_realArray = [items retain];
return self;
}
- (void)dealloc
{
[_realArray release];
[super dealloc];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"My Custom Array: %p, objs:%@", self, _realArray];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *a = @[@1, @2, @3];
NSLog(@"a: %@", a);
MyArrayClassCluster *clzCluster = [[MyArrayClassCluster alloc] initWithItems:a];
NSLog(@"clzCluster: %@", clzCluster);
}
return 0;
}
输出
2013-01-29 18:52:38.704 ClassClusterTester[31649:303] a: (
1,
2,
3
)
2013-01-29 18:52:38.707 ClassClusterTester[31649:303] clzCluster: (
1,
2,
3
)