Initializing a 2 dimensional array of a class in another
I have a class say ABC and I have to initialize a 2 dimensional array of type ABC into another class. I am bad at objective C. Tried lots of ways, but facing some errors.
Here is ----- Class ABC ------
@interface ABC : NSObject{
int a;
}
@property (nonatomic, assign) int a;
@end
@implementation ABC
@synthesize a;
@end
Here is another class say XYZ where class ABC needs to be intialized:
------ Class XYZ -----
@interface XYZ : UIView {
ABC *abc[16][16];
}
@property (nonatomic, retain) ABC *abc;
@end
@implementation XYZ
@synthesize *abc[16][16];
@end
Please suggest what could be the correct syntax of initialization. I am getting various errors everytime I 开发者_JAVA技巧try to initialize it.
If you want to use C style arrays (i.e. ABC *abc[16][16]
), you’ll need to provide accessor methods in your XYZ class.
@class ABC;
@interface XYZ : NSObject
{
ABC *abc[16][16];
}
- (void)setABC:(ABC *)anABC atRow:(NSUInteger)row column:(NSUInteger)column;
- (ABC *)abcAtRow:(NSUInteger)row column:(NSUInteger)column;
@end
@implementation XYZ
- (void)setABC:(ABC *)anABC atRow:(NSUInteger)row column:(NSUInteger)column
{
[anABC retain];
[abc[row][column] release];
abc[row][column] = anABC;
}
- (ABC *)abcAtRow:(NSUInteger)row column:(NSUInteger)column
{
return abc[row][column];
}
- (void)dealloc
{
NSUInteger row, column;
for (row = 0; row < 16; row++)
for (column = 0; column < 16; column++)
[self setABC:nil atRow:row column:column];
[super dealloc];
}
@end
You can't use [] type arrays with properties as in XYZ - what you can do would be to create an NSArray or similar container and create the multidimensional array in that way
精彩评论