Reputation: 3
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"levels.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"levels" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *levels = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSMutableArray *playLevel = (NSMutableArray *)[levels objectForKey: @"0"];
// Initialize the correct grid for answer checking.
correctGrid = [[NSMutableArray alloc] init];
for (int row = 0; row < 5; row++) {
NSMutableArray* subArr = [[NSMutableArray alloc] init];
for (int col = 0; col < 5; col++) {
int index = row * 5 + col;
int number = [playLevel objectAtIndex:index];
NSNumber *item = [NSNumber numberWithInt: number];
[subArr addObject:item];
}
[correctGrid addObject: subArr];
}
I'm new to objective-c and I'm trying to read integer values from a plist that is structured as a dictionary of arrays. Using the breakpoint/debugger in xcode shows that the plist is being successfully read. However when I try to retrieve the number from the array playLevel, each number is some ridiculously high integer such as 164911184, when the array is just all zeros. Does this have something to do with pointers? Help appreciated.
Upvotes: 0
Views: 204
Reputation: 104082
A plist can't store integers, it stores NSNumbers. So, most likely, you can change these two lines,
int number = [playLevel objectAtIndex:index];
NSNumber *item = [NSNumber numberWithInt: number];
to,
NSNumber *item = [playLevel objectAtIndex:index];
Upvotes: 1