Iphone 类的数组

Iphone 类的数组,iphone,objective-c,Iphone,Objective C,我正在objective-c中开发一个iphone应用程序,在用类变量生成数组时遇到了问题,例如 我有一个名为Cube的类,我正在尝试创建一个名为map的类的实例,它是一个数组 Cube map[10][10]; 然后Xcode说这是一个错误,并建议我这样做 Cube *map[10][10]; 当我这样做时,^虽然我无法访问我在类多维数据集中定义的方法之一,但现在这不是我的全部方法,它只是一个方法,在我尝试调用它时无法工作。这个方法与其他方法唯一不同的是我向它传递了一个参数。Cube类声明

我正在objective-c中开发一个iphone应用程序,在用类变量生成数组时遇到了问题,例如

我有一个名为
Cube
的类,我正在尝试创建一个名为map的类的实例,它是一个数组

Cube map[10][10];
然后Xcode说这是一个错误,并建议我这样做

Cube *map[10][10];
当我这样做时,^虽然我无法访问我在类
多维数据集中定义的方法之一,但现在这不是我的全部方法,它只是一个方法,在我尝试调用它时无法工作。这个方法与其他方法唯一不同的是我向它传递了一个参数。
Cube
类声明和定义的编译都完美无缺


有谁能给我解释一下,如何在不将二维对象转换为指针的情况下创建一个类。还有,为什么xcode建议我将其设置为指针,为什么这样做时该方法不起作用?

您可能希望使用Cocoa样式的数组,即
NSArray
NSMutableArray
而不是C样式的数组。它更加灵活,设计用于处理Objective-c对象。看一看这个关于使用Cocoa数组的简单教程:。

您可能希望使用Cocoa样式的数组,即
NSArray
NSMutableArray
而不是C样式的数组。它更加灵活,设计用于处理Objective-c对象。看看这个关于使用Cocoa数组的简单教程:。

你可能想看看这个答案。

你可能想看看这个答案。

在Objective-C中,这个问题的标准方法是创建一个
NSArray(或NSMutableArray)
NSArray
(或
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
            }
        }
    }
}
要访问元素,请执行以下操作:

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
            }
        }
    }
}

您可能还需要再次检查的是您试图访问的方法是否在头文件中声明。

在Objective-C中,这方面的标准方法是创建
NSArray
(或
NSMutableArray
)的
NSArray(或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
            }
        }
    }
}
要访问元素,请执行以下操作:

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
            }
        }
    }
}

您可能还需要再次检查的是,您试图访问的方法是否在头文件中声明。

如何使NSMultableArray成为特定类的数组?@kingposicle--您不会--它是NSObject的数组。那么,如果它没有实现特定类,我如何调用特定于类的方法?如何我会让NSMultableArray成为特定类的数组吗?@kingbosicle——你不会——它是NSObject的数组。那么,如果它没有实现特定类,我该如何调用特定于类的方法呢?愚蠢的objective-c为什么让我经历这个c更多?理智!我明天会尝试这段代码,它很有意义。我很愚蠢。为什么objective-c让我经历了这个c更多?理智!我明天会尝试这个代码,它让SSESEI认为你应该真正阅读Objto-C、基础和集合类的基础知识。我知道Objul-C的语法,我也知道它应该与Ccor要求无缝共享代码,因为所有Objto-C对象都必须在堆上分配。我很好奇为什么当你使用
Cube*map[10][10]
时,你的一个方法不起作用;我同意其他的说法,嵌套的NSRAPs会更好,但是也许你可以提供方法的代码,这些方法不起作用。我认为你应该真正阅读Objtovi-C、基础和集合类的基础知识。我还知道它应该与C无缝地共享代码指针要求是因为所有Objective-C对象都必须在堆上分配。我很好奇为什么当你使用
Cube*map[10][10]
时,你的一个方法不起作用;我同意其他人的观点,嵌套的nsarray会更好,但也许您可以提供一些方法代码,这些方法可以工作,也可以不工作。