0
私はコアデータを使用するアプリケーションを作成しています。私は、メインビューで使用されるレコードをユーザが作成する面を持っています。 MainViewControllerのViewWillAppearでCore Dataを取得します。問題は、新しいデバイスでアプリケーションを実行すると発生し、配列にデータがないというエラーが返されます。配列内のデータがありません
次のように私は、配列を移入:
/*
Fetch existing events.
Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Child" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];
// Order the events by creation date, most recent first.
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSSortDescriptor *prizeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"prize" ascending:NO];
NSSortDescriptor *neededDescriptor = [[NSSortDescriptor alloc] initWithKey:@"marblesneeded" ascending:NO];
NSSortDescriptor *colorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"color" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:nameDescriptor,prizeDescriptor,neededDescriptor,colorDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[nameDescriptor release];
[colorDescriptor release];
[prizeDescriptor release];
[neededDescriptor release];
[sortDescriptors release];
// Execute the fetch -- create a copy of the result.
NSError *error = nil;
records = [[self.managedObjectContext executeFetchRequest:request error:&error] retain];
if ([records count] == 0) {
recordsempty = YES;
}
[request release];
をそして、私は次のエラーを取得する:ユーザーから私のアプリをインストールする際に
-[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
誰もがこのクラッシュを防ぐ手助け上の任意のアイデアを持っていますApp Store?私は、配列が空の場合は何も表示しようとしないためにif文を追加した後
おかげ
クラッシュについての詳細や、オブジェクトを配列に配置する方法を示すコードがあれば役に立ちます。 – timthetoolman
@timthetoolman、私はコードとエラーを追加しました。 –
コードのどこにエラーが発生しているのかを示す必要があります。データがないときにのみエラーが発生する場合は、インデックス:0にデータがあることを期待するコードがあります。 –