質問

I'm building my first app, and I can't tell if this is a bug, or where the problem is. Been checking SO and the goog for hours, but can't track down this specific fix. Any help is greatly appreciated.

ViewController.h

#import "AppDelegate.m"
#import "CoreLocation/CoreLocation.h"
#import "MapKit/MapKit.h"


@interface ViewController : UIViewController 
@end

@interface CLLocationManagerDelegate : CLLocationManager
@property (strong, nonatomic) CLLocationManagerDelegate *locationManager;
@end

ViewController.m

#import "ViewController.h"


@implementation ViewController;
- (IBAction)Parked:(id)sender {
}
- (IBAction)WheresMyCar:(id)sender {
}
@end


@implementation CLLocationManagerDelegate;
{ **error is here, expected identifier or )**

-   (void)startStandardUpdates
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 250;
[locationManager startUpdatingLocation];


-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
//recent event, turn off updates to save power
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
//if event is recent, do something with it
if (abs(howRecent) <15.0) {
    NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
}
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

}
@end
役に立ちましたか?

解決

Sorry but there are so many issuea wrong with this code.

Remove the ; from the end of @implementation CLLocationManagerDelegate; and @implementation ViewController; and get rid of the { and } from @implementation ViewController; { and } @end

This method - (void)startStandardUpdates is also missing the { } brackets replace

- (void)startStandardUpdates
{
    // if(nil == locationManager) This is just silly change to below
    if (locationManager == nil) 
        locationManager = [[CLLocationManager alloc] init]; // Because there are no {} wrapping this if statement this is the only line that will run if locationManager is nil

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 250;
    [locationManager startUpdatingLocation]; 
}

Side Notes

When it comes to convention the a method should be declared with the first letter as lower case so the below methods

- (IBAction)Parked:(id)sender { }
- (IBAction)WheresMyCar:(id)sender { }

should become

- (IBAction)parked:(id)sender { }
- (IBAction)wheresMyCar:(id)sender { }

Since your subclassing CLLocationManager I wouldn't recommend using the name CLLocationManagerDelegate this could create some confusion. I would recommend something along the lines of just @interface LocationManager : CLLocationManager or prefix it with the something else like company initials, so for the company I work for this would become @interface PPDLocationManager : CLLocationManager Also the reason behind why you shouldn't be using the name CLLocationManagerDelegate as your class is because it is already a protocol name see the Apple Documentation for CLLocationManagerDelegate

The Below methods are in the wrong @implementation

- (void)viewDidLoad
- (void)didReceiveMemoryWarning

they should be in your ViewController implementation not your CLLocationManagerDelegate implementation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top