1

データの保存:

- (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 を中心にしています。理由はありますか?

4

3 に答える 3

2

最も簡単な方法は、NSNumberintValueメソッドを使用することです。

loadint = [[array objectAtIndex:0] intValue];
于 2009-09-04T04:51:13.910 に答える
1

以下の 2 行目が必要です。

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

値を 1 つだけ保存する場合は、保存する値に簡単にラベルを付けることができるキーを持つ NSDictionary を使用します。

于 2009-09-04T04:55:05.570 に答える
0

CGPointMake は次のように定義されています。

CGPoint CGPointMake(
    float x,
    float y
)

おそらく float である GreenBlot.center.x を取り、それを int として NSNumber に供給しています...戻って変数のタイプを見て、 [NSNumber numberWithFloat:...] と lx = [ [配列 objectAtIndex:1] floatValue]; 等、適宜。

また、元の質問の編集で別の質問を作成しました。

于 2009-09-04T11:43:47.447 に答える