如何正确初始化对象? (在我的情况下CLLocation)

时间:2013-08-08 11:50:30

标签: ios objective-c cllocation

我正在尝试使用点和距离填充我的tableView到当前位置。

我在初始化属性方面遇到了问题。

在.h文件中:

#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

在.m文件中:

#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

currentLocation为空:

currentLocation CLLocation *    0x00000000

我试过写viewDidLoad

currentLocation = [[CLLocation alloc] init];

但这对我没有帮助。

内存为对象分配,但是对象在没有_latitude和_longitude属性的情况下创建 我做错了什么?

0 个答案:

没有答案