关于ios:将保存在plist中的NSNumbers转换为整数

Converting NSNumbers saved in a plist to integers

保存数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (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将显示高分。 我收到一个警告:"赋值在没有强制转换的情况下从指针生成整数"

1
2
3
4
5
6
7
8
9
10
11
12
13
-(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转换,但这似乎是不必要的。 谢谢你的帮助!

编辑:

1
- (void)applicationWillTerminate:(NSNotification *)notification

{
NSMutableArray * array = [[NSMutableArray alloc] init];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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];

现在加载:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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为中心。 知道为什么吗?


最简单的方法是使用NSNumberintValue方法:

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


你需要下面的第二行:

1
2
3
loadint = [array objectAtIndex:0];
int score = [loadint intValue];
NSString *myString35 = [NSString stringWithFormat:@"%d", loadint];

如果您只保存一个值,则使用带有键的NSDictionary,以便您更容易标记要保存的值。


CGPointMake定义如下:

1
2
3
4
CGPoint CGPointMake(
    float x,
    float y
)

您正在使用GreenBlot.center.x,它可能是一个浮点数并将其作为一个int提供给NSNumber ....返回查看变量的类型并更改为[NSNumber numberWithFloat:...]和lx = [ [array objectAtIndex:1] floatValue]; 等等,视情况而定。

此外,您在编辑原始问题时创建了一个不同的问题。