gpt4 book ai didi

ios - 将保存在plist中的NSNumbers转换为整数

转载 作者:行者123 更新时间:2023-12-01 19:29:17 25 4
gpt4 key购买 nike

保存数据:

- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];

NSNumber *number = [NSNumber numberWithInt:points];
[array addObject:number];

[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}

读取数据:
BestLabel.text将显示高分。我收到警告:“赋值从指针进行整数转换而无需强制转换”
-(void)LoadData{
BestLabel.center = CGPointMake(150,300);

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
loadint = [array objectAtIndex:0];
NSString *myString35 = [NSString stringWithFormat:@"%d", loadint];
BestLabel.text = myString35;
[array release];
}
}

我从中得到的示例代码是毫无问题地保存和加载文本字段。.我想我可以将字符串转换为int,但这似乎没有必要。谢谢你的帮助!

编辑:
- (void)applicationWillTerminate:(NSNotification *)notification

{
NSMutableArray * array = [[NSMutableArray alloc] init];
if (points > highscore){

NSString *myString35 = [NSString stringWithFormat:@"%d", points];
[array addObject:myString35];
}else {
NSString *myString35 = [NSString stringWithFormat:@"%d", highscore];
[array addObject:myString35];
}


NSNumber *number = [NSNumber numberWithInt:GreenBlot.center.x];
[array addObject:number];
NSNumber *number2 = [NSNumber numberWithInt:GreenBlot.center.y];
[array addObject:number2];

现在加载:
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
BestLabel.text = [array objectAtIndex:0];
highscore = [[array objectAtIndex:0] intValue];


lx = [[array objectAtIndex:1] intValue];
ly = [[array objectAtIndex:2] intValue];
GreenBlot.center = CGPointMake( lx,ly);


[array release];
}

lx和ly是整数。我首先尝试了CGPointMake([[array objectAtIndex:1] intValue],[[array objectAtIndex:2] intValue])。同样的结果,GreenBlot正以0,0为中心。知道为什么吗?

最佳答案

最简单的方法是使用NSNumber intValue 方法:

loadint = [[array objectAtIndex:0] intValue];

关于ios - 将保存在plist中的NSNumbers转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1377216/

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