Unable to access a primitive array inside a custom class from a UIViewController instance
Posted
by Daniel
on Stack Overflow
See other posts from Stack Overflow
or by Daniel
Published on 2010-04-06T03:37:04Z
Indexed on
2010/04/06
3:43 UTC
Read the original article
Hit count: 527
Hello!
I have made a subclass of NSObject that is meant to be the model of my application. The class has a few methods and on instance primitive array as such:
@interface Cube : NSObject {
int cubeState[5][2][2];
}
- (id)printContent;
@end
@implementation Cube
- (void)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
This works fine if instanciated from the delegate as such:
#include "Cube.h"
@implementation CubeAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
Cube *cube = [[Cube alloc] init];
[cube printContent];
[cube release];
[window makeKeyAndVisible];
}
However, the application crashes if I try to create a subclass of UIViewController with a Cube *cube property and later try to access the Cube instance object through the view controller's property as such:
@interface CustomController : UIViewController {
Cube *cube;
}
@property (nonatomic, retain) Cube *cube;
@end
@implementation CustomController
@synthesize cube;
- (void)dealloc {
[cube release];
[super dealloc];
}
@end
and in the delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
viewController = [[CustomController alloc]
initWithNibName:@"MainView" bundle:nil];
viewController.cube = [[[Cube alloc] init] autorelease];
[viewController.cube printContent]; // Application crashes here
}
Any ideas?
© Stack Overflow or respective owner