目标C中的继承问题 [英] Inheritance Issues in Objective C

查看:195
本文介绍了目标C中的继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个SDMutableGrid类,以便我可以使用网格。它只是NSMutableArray的一个子节点,它包含一个数字,用于等于网格中的行数。

I created an "SDMutableGrid" class so that I could use a grid. It's just a child of NSMutableArray that contains a number for arrays equal to the number of rows in the grid.

目前,程序在它真正启动之前退出并且它看起来像这是因为为NSMutableArray定义的方法不知何故不适用于SDMutableGrid,任何人都知道为什么?

Currently, the program quits before it really starts and it appears that it is because the methods defined for NSMutableArray somehow do not apply to SDMutableGrid, anyone know why?

这是.h:

#import <Foundation/Foundation.h>
#import "SDDimensions.h"

@interface SDMutableGrid : NSMutableArray {
SDDimensions dimensions;
}

@property (nonatomic) SDDimensions dimensions;

- (id)initWithDimensions:(SDDimensions)newDimensions;
- (void)addObject:(id)anObject toRow:(NSUInteger)row;

@end

这是.m:

#import "SDMutableGrid.h"

@implementation SDMutableGrid

@synthesize dimensions;

- (void)setDimensions:(SDDimensions)newDimensions {
if (newDimensions.width < dimensions.width) {
    NSMutableArray *anArray;
    NSRange aRange = NSMakeRange(newDimensions.width, dimensions.width - newDimensions.width);
    for (NSUInteger i = 0; i < MIN(dimensions.height,newDimensions.height); i++) {
        anArray = [self objectAtIndex:i];
        [anArray removeObjectsInRange:aRange];
    }
}
dimensions.width = newDimensions.width;
if (newDimensions.height > dimensions.height) {
    for (NSUInteger i = dimensions.height; i < newDimensions.height; i++) {
        [self addObject:[[NSMutableArray alloc] initWithCapacity:dimensions.width]];
    }
} else if (newDimensions.height < dimensions.height) {
    [self removeObjectsInRange:NSMakeRange(newDimensions.height, dimensions.height - newDimensions.height)];
}
dimensions.height = newDimensions.height;
}

- (id)initWithDimensions:(SDDimensions)newDimensions {
if (self = [super initWithCapacity:newDimensions.height]) {
    NSMutableArray *anArray;
    for (NSUInteger i = 0; i < newDimensions.height; i++) {
        anArray = [[NSMutableArray alloc] initWithCapacity:newDimensions.width];
        NSLog(@"Got this far");
        [self addObject:anArray];
        NSLog(@"woot");
        [anArray release];
    }
    NSLog(@"Finished Initializing grid");
}
return self;
}

- (void)addObject:(id)anObject toRow:(NSUInteger)row {
    [[self objectAtIndex:row] addObject:anObject];
}

@end

以下是出现的内容在控制台上:

And here is what is appearing on the console:

2009-08-12 15:27:02.076 Flipswitch [1756:20b] 由于未被捕获而终止应用异常'NSInvalidArgumentException',原因:' - [NSMutableArray initWithCapacity:]:仅为抽象类定义的方法。定义 - [SDMutableGrid initWithCapacity:]!'
2009-08-12 15:27:02.080 Flipswitch [1756:20b] Stack:(
807902715,
2536648251,
808283725,
808264737,
13690,
11018,
10185,
814713539,
814750709,
814739251,
814722434,
814748641,
839148405,
807687520,
807683624,
814715661,
814752238,
10052,
9906

2009-08-12 15:27:02.076 Flipswitch[1756:20b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define -[SDMutableGrid initWithCapacity:]!' 2009-08-12 15:27:02.080 Flipswitch[1756:20b] Stack: ( 807902715, 2536648251, 808283725, 808264737, 13690, 11018, 10185, 814713539, 814750709, 814739251, 814722434, 814748641, 839148405, 807687520, 807683624, 814715661, 814752238, 10052, 9906 )

推荐答案

简单明了的答案:不要创建NSArray的子​​类。最好在NSArray上创建一个类别,或者创建一个与你交谈的NSArray ivar的NSObject子类。

The short, easy answer: Don't make a subclass of NSArray. It's better to make a category on NSArray or make an NSObject subclass that has an NSArray ivar that you talk to.

长期的技术答案:NSArray是一个类群集。这意味着它实际上不是一个类,而是在NSArray抽象类接口下运行的许多类,每个类都以不同的方式实现(例如,一个实现小型数组,另一个实现大型数组等)。要创建类集群的子类,您必须实现您继承的抽象类的所有原始方法,管理您自己的存储,并基本上重新实现您希望通过子类化免费获得的所有内容。

The long, technical answer: NSArray is a class cluster. This means that it isn't actually one class, but many classes operating under the NSArray abstract class interface that are each implemented in a different way (say, one implementation for small arrays, another for big arrays, etc.). To create a subclass of a class cluster, you have to implement all the primitive methods of the abstract class you are inheriting from, manage your own storage and basically reimplement all the stuff you were hoping to get for free by subclassing.

更简单地说,如果你不需要额外的ivars,你可以创建一个类别。如果您想要一个行为类似于具有附加状态的数组的对象,您可以创建一个具有NSArray的类,并使用Objective-C消息转发将除自定义行为之外的所有内容转发到该类。

More simply, you could just create a category if you don't require additional ivars. If you want an object that behaves like an array with additional state, you can create a class that has an NSArray and use Objective-C message forwarding to forward everything except your custom behavior to that class.

这篇关于目标C中的继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆