データの保存:
- (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 を中心にしています。理由はありますか?