将 plist 中保存的 NSNumber 转换为整数

发布于 2024-08-03 18:53:06 字数 2428 浏览 11 评论 0原文

保存数据:

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

Saving data:

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

Reading data:
BestLabel.text will display the high score. I get a warning: "assignment makes integer from pointer without a cast"

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

The sample code I got this from was saving and loading text fields with no problem.. I suppose I could do a string to int conversion, but that seems unnecessary. Thanks for your help!

EDIT:

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

And now loading with:

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 and ly are integers. I tried CGPointMake( [[array objectAtIndex:1] intValue], [[array objectAtIndex:2] intValue] ) first. Same results, GreenBlot is getting centered at 0,0. Any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七色彩虹 2024-08-10 18:53:06

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

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

The easiest thing to do is use NSNumber's intValue method:

loadint = [[array objectAtIndex:0] intValue];
情徒 2024-08-10 18:53:06

您需要下面的第二行:

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

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

You need the second line below:

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

If you are only saving one value, then use a NSDictionary with a key that makes it easier for you to label the value you are saving.

留一抹残留的笑 2024-08-10 18:53:06

CGPointMake 的定义是:

CGPoint CGPointMake(
    float x,
    float y
)

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

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

CGPointMake is defined:

CGPoint CGPointMake(
    float x,
    float y
)

You are taking GreenBlot.center.x which is presumably a float and feeding it to NSNumber as an int.... Go back and look at the types of your variables and change to [NSNumber numberWithFloat:...] and lx = [[array objectAtIndex:1] floatValue]; etc., as appropriate.

Also, you've created a different question in your editing of the original question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文