【问题标题】:Converting NSNumbers saved in a plist to integers将保存在 plist 中的 NSNumber 转换为整数
【发布时间】:2009-09-04 04:22:59
【问题描述】:

保存数据:

- (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 为中心。知道为什么吗?

【问题讨论】:

  • loadint 是 int 还是 NSNumber?

标签: ios objective-c plist nsnumber


【解决方案1】:

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

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

【讨论】:

  • 试试这个.. 见主帖。尝试使用 intValues 设置 CGPointMake 只会给我 0,0。
  • 1. GreenBlot.center.x 返回什么类型的变量? 2、lx和ly是什么类型的变量?
【解决方案2】:

你需要下面的第二行:

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

如果您只保存一个值,请使用带有键的 NSDictionary,以便您更轻松地标记要保存的值。

【讨论】:

  • 小修正:[loadint integerValue] 返回一个NSInteger,它(取决于系统)可能是也可能不是int。您要么希望将方法调用更改为intValue,要么将score 的类型更改为NSInteger
【解决方案3】:

CGPointMake 已定义:

CGPoint CGPointMake(
    float x,
    float y
)

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多