0

プログラムでは、このメソッドを使用して、保存できるファイルを作成します。

-(NSString*) saveFilePath{
NSString* path = [NSString stringWithFormat:@"%@%@",
                  [[NSBundle mainBundle] resourcePath],
                  @"savingfile.plist"];
return path;}

次に、ボタンを使用して、データをファイルに入れるプロセスを開始しました (最初にすべてを配列に入れて、簡単にするようにしました)。

- (IBAction)save
{

NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:name.text];
[myArray addObject:position.text];
[myArray addObject:cell.text];
[myArray addObject:office.text];
[myArray addObject:company.text];
[myArray writeToFile:[self saveFilePath] atomically:YES];

}

最後に、情報を - (void)viewDidAppear メソッドのテキスト フィールドにロードし直しました。

- (void)viewDidAppear:(BOOL)animated
{
NSMutableArray* myArray = [NSMutableArray arrayWithContentsOfFile:[self saveFilePath]];
name.text = [myArray objectAtIndex:0];
position.text = [myArray objectAtIndex:1];
cell.text = [myArray objectAtIndex:2];
office.text = [myArray objectAtIndex:3];
company.text = [myArray objectAtIndex:4];
}

何らかの理由で、シミュレーターではシミュレーターで完全に動作していますが、物理的な iPhone で実行しようとするとまったく動作しません。

4

1 に答える 1

3

iOSで読み取り専用の場所に保存しようとしていると思います。シミュレーターは実際のハードウェアでサンドボックス環境を完全に複製しないため、シミュレーターで機能します。

に保存するのではなくresourcesPath、ファイルをDocumentsディレクトリ(または適切な場合はキャッシュディレクトリ)に保存する必要があります。

次のようにして、documentsディレクトリへのパスを取得できます。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

この質問の詳細については、Objective-cのアプリのリソースフォルダーにファイルを保存する方法をご覧ください。

于 2012-07-11T15:44:21.133 に答える