开发者

Simulator not getting location

I'm following this tutorial to learn about Core Data. I'm currently on section 4, Adding Events. In section 2 the tutorial says that the Add Button will become active seconds after I am prompted by the Simulator to get my location, but it never becomes active, any idea what might be happening?

PS: If I hardcode the button to be active before getting location I get the following error:

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

On the following line:

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

EDIT: Posting more code to get a bigger picture

- (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];

}

This is the method where I get there error:

-(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] atScrollP开发者_开发百科osition:UITableViewScrollPositionTop animated:YES];

}

And my appDidLaunch method:

- (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;
}


There is no NSMutableArray in the code you posted. The problem from the error is that you are trying to access an object (at index 0) in an empty NSMutableArray. Find that array, and you will probably find the problem.


You're not getting a CLLocation object. In the third line of the addEvent method you have if(location) { return } so basically every time you successfully get a location object from CLLocation *location = [locationManager location]; you just return without executing the rest of the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜