Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 无法从UIViewController实例访问自定义类中的基元数组_Objective C_Arrays_Class - Fatal编程技术网

Objective c 无法从UIViewController实例访问自定义类中的基元数组

Objective c 无法从UIViewController实例访问自定义类中的基元数组,objective-c,arrays,class,Objective C,Arrays,Class,我已经创建了NSObject的一个子类,它是我的应用程序的模型。该类有几个方法和实例基元数组,如下所示: @interface Cube : NSObject { int cubeState[5][2][2]; } - (void)printContent; @end @implementation Cube - (id)init { if (self = [super init]) { for (int i=0; i<=5; i++) {

我已经创建了NSObject的一个子类,它是我的应用程序的模型。该类有几个方法和实例基元数组,如下所示:

@interface Cube : NSObject {
    int cubeState[5][2][2];
}

- (void)printContent;

@end

@implementation Cube


- (id)init {
    if (self = [super init]) {
        for (int i=0; i<=5; i++) {
            for (int j=0; j<=2; j++) {
                for (int k=0; k<=2; k++) {
                    cubeState[i][j][k] = i;
                }
            }
        }
    }
    return self;
}


- (void)printContent {
    for (int i=0; i<=5; i++) {
        for (int j=0; j<=2; j++) {
            for (int k=0; k<=2; k++) {
                NSLog(@"[%d] [%d] [%d] = %d", i, j, k, cubeState[i][j][k]);
            }
        }
    }
}

@end
@interface CustomController : UIViewController {
    Cube *cube;
}

@property (nonatomic, retain) Cube *cube;

@end

@implementation CustomController

@synthesize cube;

- (void)dealloc {
    [cube release];
    [super dealloc];
}
@end
但是,如果我尝试使用Cube*Cube属性创建UIViewController的子类,然后尝试通过视图控制器的属性访问多维数据集实例对象,则应用程序会崩溃,如下所示:

@interface Cube : NSObject {
    int cubeState[5][2][2];
}

- (void)printContent;

@end

@implementation Cube


- (id)init {
    if (self = [super init]) {
        for (int i=0; i<=5; i++) {
            for (int j=0; j<=2; j++) {
                for (int k=0; k<=2; k++) {
                    cubeState[i][j][k] = i;
                }
            }
        }
    }
    return self;
}


- (void)printContent {
    for (int i=0; i<=5; i++) {
        for (int j=0; j<=2; j++) {
            for (int k=0; k<=2; k++) {
                NSLog(@"[%d] [%d] [%d] = %d", i, j, k, cubeState[i][j][k]);
            }
        }
    }
}

@end
@interface CustomController : UIViewController {
    Cube *cube;
}

@property (nonatomic, retain) Cube *cube;

@end

@implementation CustomController

@synthesize cube;

- (void)dealloc {
    [cube release];
    [super dealloc];
}
@end
在代表中:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    viewController = [[CustomController alloc] 
                        initWithNibName:@"MainView" bundle:nil];
    viewController.cube = [[[Cube alloc] init] autorelease];

    [viewController.cube printContent]; // Application crashes here
}
有什么想法吗?

(1)

-init
必须返回
id
。由于返回的是
void
,语句

[[[Cube alloc] init] autorelease];
可以归还一些乱七八糟的东西

当然

- (id)printContent;

这应该返回
void

KennyTM!非常感谢你,你成功了。我认为声明数组大小时的索引是最大索引,而不是项数。就id/void而言,是的,是的,绝对是的,我很快就输入了一些虚拟函数,这超出了我的理解范围。无论如何,谢谢你!什么事?怎么了?如果在调试器中单步进入printContent会怎么样?你能说出这次车祸的确切情况吗?
- (id)printContent;