gpt4 book ai didi

ios - 尝试从 plist 中读取整数会导致意外结果

转载 作者:行者123 更新时间:2023-11-29 03:27:08 25 4
gpt4 key购买 nike

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

我是 Objective-C 的新手,我正在尝试从结构为数组字典的 plist 中读取整数值。在 xcode 中使用断点/调试器显示 plist 已成功读取。然而,当我尝试从数组 playLevel 中检索数字时,每个数字都是一些高得离谱的整数,例如 164911184,而数组全是零。这和指针有关系吗?感谢帮助。

最佳答案

plist 不能存储整数,它存储 NSNumber。因此,最有可能的是,您可以更改这两行,

int number = [playLevel objectAtIndex:index];
NSNumber *item = [NSNumber numberWithInt: number];

到,

NSNumber *item = [playLevel objectAtIndex:index];

关于ios - 尝试从 plist 中读取整数会导致意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com