2011-06-30 11 views
0

Core Dataについては、this tutorialに従っています。私は現在、セクション4、イベントの追加に入っています。セクション2では、チュートリアルでは、シミュレータが私の位置を取得するよう促すメッセージが表示された後、追加ボタンがアクティブになると表示されますが、アクティブになることはありません。シミュレータが場所を取得しない

PS:

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 

EDIT:取得するために多くのコードを投稿の次の行に

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array' 

:私は場所を取得する前にアクティブにするボタンをハードコードした場合、私は次のエラーを取得します大きな画像

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.title = @"Locations"; 

    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)]; 
    addButton.enabled = NO; 
    self.navigationItem.rightBarButtonItem = self.addButton; 

    [[self locationManager] startUpdatingLocation]; 

    eventsArray = [[NSMutableArray alloc] init]; 

} 

これはエラーが表示される方法です:

-(void)addEvent{ 
    CLLocation *location = [locationManager location]; 

    if(location){ 
     return; 
    } 

    Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext]; 

    CLLocationCoordinate2D coordinate = [location coordinate]; 
    event.latitude = [NSNumber numberWithFloat:coordinate.latitude]; 
    event.longitude = [NSNumber numberWithFloat:coordinate.longitude]; 
    event.creationDate = [NSDate date]; 

    NSError *error; 
    if (![managedObjectContext save:&error]) { 

    } 

    [eventsArray insertObject:event atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

} 

そして、私のappDidLaunch方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain]; 
    navigationController = [[UINavigationController alloc] init]; 

    NSManagedObjectContext *context = [self managedObjectContext]; 

    if(!context){ 

    } 

    rootViewController.managedObjectContext = context; 

    [self.navigationController pushViewController:rootViewController animated:NO]; 

    [rootViewController release]; 

    [self.window addSubview:navigationController.view]; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

答えて

1

あなたが投稿したコードにはNSMutableArrayはありません。エラーの問題は、NSMutableArrayの空のオブジェクト(インデックス0)にアクセスしようとしていることです。その配列を見つけると、おそらく問題が見つかります。

+0

私のコードを他の関連するメソッドで更新しました。私のviewDidLoadは、私のRootViewControllerのプロパティであるNSMutableArrayを割り当てます。 – 8vius

0

CLLocationオブジェクトを取得していません。 addEventメソッドの3行目には、(location){ return } というように、基本的にCLLocation * location = [locationManager location]からロケーションオブジェクトを取得するたびに取得されます。残りのメソッドを実行せずに戻ります。

関連する問題