0

I am trying to fill my tableView with a points and distances to current location.

I've got a problem with initialize property.

In .h file:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface SIBViewController : UIViewController
<CLLocationManagerDelegate>
{
    NSArray *_data;
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}

@property (nonatomic, retain) CLLocation *currentLocation;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

In .m file:

#import "SIBViewController.h"
#import "atmCell.h"
#import "sibAtmData.h"

@interface SIBViewController ()

@end


@implementation SIBViewController

@synthesize currentLocation;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self startSignificantChangeUpdates];

    _data = [sibAtmData fetchData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"atmCell";

    atmCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    sibAtmData *item = [_data objectAtIndex:indexPath.row];

    cell.titleLabel.text = item.title;
    cell.subtitleLabel.text = item.subtitle;

    CLLocationDistance distance = [self.currentLocation distanceFromLocation:item.location];

    cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km", distance/1000];

    NSLog(@"distance: %f", distance);

    return cell;

}


- (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    [locationManager startMonitoringSignificantLocationChanges];
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // If it's a relatively recent event, turn off updates to save power
    self.currentLocation = [locations lastObject];
    NSDate* eventDate = currentLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {

        [self.tableView reloadData];

        // If the event is recent, do something with it.
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
              currentLocation.coordinate.latitude,
              currentLocation.coordinate.longitude);
    }


}

@end

But currentLocation is empty:

currentLocation CLLocation *    0x00000000

I've tried to write in viewDidLoad:

currentLocation = [[CLLocation alloc] init];

but this didn't help me.

Memory allocates for object, but object creates without _latitude and _longitude properties What am I doing wrong?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Aleksandr K.
  • 1,338
  • 14
  • 21
  • How would you use the location if you don't know the lat/long? Or do you want to use some default values? – Wain Aug 08 '13 at 11:53
  • @Wain, if I initialize currentLocation in didUpdateLocations CLLocation *currentLocation = [locations lastObject]; it works properly, but I want to use this variable in cellForRowAtIndexPath: CLLocationDistance distance = [currentLocation distanceFromLocation:item.location]; cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km", distance/1000]; – Aleksandr K. Aug 08 '13 at 11:55
  • Make it a property and a strong one if you are using ARC. – Puneet Sharma Aug 08 '13 at 11:58
  • 2
    self.currentLocation and currentLocation mean 2 different things in your case. Synthesize _currentLocation to currentLocation to fix this. – Stavash Aug 08 '13 at 12:00
  • Stavash is completely right. Read this to know more: http://stackoverflow.com/a/17784167/269753 – Ricardo Sanchez-Saez Aug 08 '13 at 12:03
  • @Stavash, I've replaced listing. There is all code in file. But I don't understand, what I have to do to make it work. – Aleksandr K. Aug 08 '13 at 12:11
  • 2
    Remove the iVar declaration. Access it by self.currentLocation. That's it. – Stavash Aug 08 '13 at 12:15
  • 1
    "Find my bug" question has no future value to SO. – Marcus Adams Aug 08 '13 at 13:03

0 Answers0