私は、保存されたファイルを使用してAppが起動するたびに2つのデータを読み込むアプリを持っています。完璧に動作します。私のAppの新しいバージョンでは、私は3番目のデータを追加したいと思います。私はそれを実行したときしかし、私はこのエラーを取得する:iPhoneに保存されている既存の保存されたNSMutableArrayに新しいデータを追加する
がキャッチされない例外により 'NSRangeException' にアプリを終了、理由: '*- [__ NSArrayM objectAtIndex:]:インデックスが範囲を越えて2 [0 .. 1]
起動時にファイルを読み込むときに、既存の保存ファイルに2つしか含まれていない場合は、3番目のデータが呼び出されるためです。
ここに私のコードです:二つのデータと既存のNSMutableArrayのファイルがすでに存在しているため、問題は右の方法の最後に発生
- (void)viewDidLoad {
//---Check for existence of the Archive File on Startup-
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
dataFilePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"data.archive"]];
// Check if the file already exists, and if so, loads the data
if ([filemgr fileExistsAtPath:dataFilePath]) {
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath];
defaultValueTextField.text = [dataArray objectAtIndex:0];
segment = [[dataArray objectAtIndex:1] intValue];
roundingSegment = [[dataArray objectAtIndex:2] intValue];
NSLog(@" Segment is %d", segment);
NSLog(@" RoundingSegment is %d", roundingSegment);
}
[super viewDidLoad];
}
と私のコード:
int roundingSegment = 1;
、その後、 THIRD、すなわち 'roundingSegment'変数を読み込もうとしています。 (どこにコードがインデントされていますか?)
新しいバージョンのアプリケーションを読み込むときにユーザーが保存した既存のNSMutableArrayを削除する必要がありますか?そしてもしそうなら、どう?
または、私の 'roundingSegment'変数を取得しようとするとハングしないように、Appの最初の実行時に3番目のデータを追加して追加する方法がありますか?
あなたが貸すことができれば、大変感謝します。