2011-06-30 3 views
0

Я следую this tutorial, чтобы узнать о Core Data. Я сейчас в разделе 4 «Добавление событий». В разделе 2 в учебнике говорится, что кнопка «Добавить» станет активной секундой после того, как я попрошу симулятор получить мое местоположение, но он никогда не становится активным, любая идея, что может произойти?Симулятор не получает местоположение

PS: Если я жёстко кнопку, чтобы быть активным перед тем, как местоположение, я получаю следующее сообщение об ошибке:

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

На следующей строке:

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

EDIT: Проводка больше кода, чтобы получить большая картина

- (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. Проблема из-за ошибки в том, что вы пытаетесь получить доступ к объекту (по индексу 0) в пустой NSMutableArray. Найдите этот массив, и вы, вероятно, найдете проблему.

+0

Я обновил свой код с помощью других соответствующих методов в своем представленииDidLoad, где я выделяю NSMutableArray, который является свойством моего RootViewController – 8vius

0

Вы не получаете объект CLLocation. В третьей строке метода addEvent у вас есть if (location) { return } } , так что каждый раз, когда вы успешно получаете объект местоположения из CLLocation * location = [locationManager location]; вы просто возвращаетесь без выполнения остальной части метода.

Смежные вопросы