Objective-C 中的标准方法是创建NSArray(or NSMutableArray)
一个NSArray
(or NSMutableArray
)。假设您希望在创建数组对象后能够操作数组,您的代码将如下所示:
NSMutableArray* cubeGrid = [NSMutableArray new]; // Note that this code assumes you are using ARC.
// add row 1
NSMutableArray* cubeRow1 = [NSMutableArray arrayWithObjects:cube1,cube2,cube3,nil]; // you will need to add cube 4 to 10 in the real code
[cubeGrid addObject:cubeRow1];
// add row 2
NSMutableArray* cubeRow2 = [NSMutableArray arrayWithObjects:cube11,cube12,cube13,nil]; // you will need to add cube 14 to 20 in the real code
[cubeGrid addObject:cubeRow2];
// and you will create the rest of the rows and add to the cubeGrid array
要访问元素,您将执行以下操作:
for (id cubeRow in cubeGrid) {
if ([cubeRow isKindOfClass:[NSArray class]]) {
for (id cube in (NSArray*)cubeRow) {
if ([cube isKindOfClass:[Cube class]]) {
// Do things with cube
}
}
}
}
您可能还需要仔细检查您尝试访问的方法是否在头文件中声明。