Your singleton looks wrong. Your sharedInstance method should be.
Up vote 0 down vote favorite share g+ share fb share tw.
I decided to try and make a Singleton for use with locations. I have got what I think to be, the singleton working correctly however I have one error that is now appearing. It tells me that my implementation is incomplete.
It is the only error it is giving me and I am sure it is something wrong with either my view header or m file. I have tried a few things now and cannot get it to work. What am I missing here?
Here is my header #import @interface TrackerViewController : UIViewController -(void) locationUpdate; end And here is my implementation file: #import "TrackerViewController. H" #import "MyLocation. H" @implementation TrackerViewController NSString *LatCoord; NSString *LongCoord; - (void)didReceiveMemoryWarning { super didReceiveMemoryWarning; // Release any cached data, images, etc that aren't in use.
} #pragma mark - View lifecycle - (void)viewDidLoad { super viewDidLoad; LocationController sharedInstance. LocDelegate = (id)self; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { super viewDidUnload; // Release any retained subviews of the main view.
// e.g. Self. MyOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { super viewWillAppear:animated; } - (void)viewDidAppear:(BOOL)animated { super viewDidAppear:animated; } - (void)viewWillDisappear:(BOOL)animated { super viewWillDisappear:animated; } - (void)viewDidDisappear:(BOOL)animated { super viewDidDisappear:animated; } - (void)locationUpdate:(CLLocation *)location { LocationController sharedInstance setLocation:location; LatCoord = NSString stringWithFormat:@"%lf", location.coordinate. Latitude; LongCoord = NSString stringWithFormat:@"%lf", location.coordinate.
Longitude; } - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } -(IBAction)CheckIn:(id)sender { self locationUpdate; } @end My singleton header is as follows: #import #import @protocol LocationControllerDelegate @required - (void)locationUpdate:(CLLocation*)location; @end @interface LocationController : NSObject { __unsafe_unretained id _locDelegate; CLLocationManager* locationManager; CLLocation* location; id locDelegate; } @property (nonatomic, retain) CLLocationManager* locationManager; @property (nonatomic, retain) CLLocation* location; @property (nonatomic, assign) id locDelegate; + (LocationController*)sharedInstance; @end My singleton implementation is as follows: #import "MyLocation. H" static LocationController* sharedCLDelegate = nil; @implementation LocationController @synthesize locationManager, location, locDelegate = _locDelegate; - (id)init { self = super init; if (self! = nil) { self.
LocationManager = CLLocationManager alloc init; self.locationManager. Delegate = self; self.locationManager. DesiredAccuracy = kCLLocationAccuracyBest; } return self; } - (void)dealloc { /* ... */ } #pragma mark - #pragma mark CLLocationManagerDelegate Methods - (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation { /* ... */ } - (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error { /* ... */ } #pragma mark - #pragma mark Singleton Object Methods + (LocationController*)sharedInstance { @synchronized(self) { if (sharedCLDelegate == nil) { } } return sharedCLDelegate; } + (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (sharedCLDelegate == nil) { sharedCLDelegate = super allocWithZone:zone; return sharedCLDelegate; } } return nil; } - (id)copyWithZone:(NSZone *)zone { return self; } @end What am I missing or doing wrong here?
Ios xcode ios5 xcode4.2 link|improve this question asked Feb 4 at 12:24JESLAB184 90% accept rate.
I think that __weak would be the better choice. – Richard J. Ross III Feb 4 at 13:39 __weak doesn't work on iOS 4, so most people would use __unsafe_unretained for this.
– Nick Lockwood Feb 4 at 17:32.
Your singleton looks wrong. Your sharedInstance method should be: + (LocationController*)sharedInstance { @synchronized(self) { if (sharedCLDelegate == nil) { sharedCLDelegate = self alloc init; } } return sharedCLDelegate; } And you can get rid of the allocWithZone: and copyWithZone: methods - they don't do anything useful. That's most likely why your class isn't working, but probably has nothing to do with the "incomplete implementation" warning.
The warning will be because you've forgotten to implement a method that's declared in your header or in one of your protocols, or maybe you've misspelled it. I couldn't spot it on a first pass, but I'll take another look. If you double-click on the yellow warning icon in the header it should tell you which method hasn't been implemented properly.
By the way, the line below leaks because you're double-retaining (init sets retain count to one, and then assigning to a retained property sets it to 2). You should run the Analyze function, which will add blue warnings for leaks like this. Self.
LocationManager = CLLocationManager alloc init; Do one of the following instead: //assign directly to the ivar so the setter method isn't used locationManager = CLLocationManager alloc init; Or //autorelease before assigning self. LocationManager = CLLocationManager alloc init autorelease.
2 You've explained how to implement a shared object, but not a singleton. A singleton is a class that cannot be instantiated more than once. The point of overriding -allocWithZone: and -copyWithZone: is to enforce the single instantiation rule.
– Caleb Feb 4 at 12:54 Yes, technically, but most people who say they want a singleton actually want a shared instance, and judging by this code, that's what JESLAB wanted. – Nick Lockwood Feb 4 at 17:31 Okay, I have gone according to what you and @Caleb said but I still cannot get it to start the location services even though now there is no error in my code at all. Is there a decent tutorial on singletons and locations that you could recommend?
– JESLAB Feb 5 at 7:53 Thanks to all that answered. I learnt a bit from each of you. I chose Nick's as the best answer as it was closer to what I actually was trying to do.
– JESLAB Feb 7 at 9:24.
I think the root of the problem is here: @protocol LocationControllerDelegate @required - (void)locationUpdate:(CLLocation*)location; @end So you've defined a protocol with a required method, but unless I've missed it you haven't implemented that method anywhere in the object that adopts that protocol. The compiler is warning you that you haven't implemented all the required methods of the protocol.
1 The method definition has no parameters; the implementation has one parameter. The code also calls self locationUpdate with no parameter; that won't do anything. – bneely Feb 4 at 13:10 I don't see any classes in the code that implement LocationControllerDelegate, so you wouldn't expect to see it implemented anywhere.
– Nick Lockwood Feb 4 at 17:34 Okay, I have gone according to what you and @NickLockwood said but I still cannot get it to start the location services even though now there is no error in my code at all. Is there a decent tutorial on singletons and locations that you could recommend? – JESLAB Feb 5 at 7:53.
LocationUpdate is declared in your interface, but not defined in the implementation. LocationUpdate: is defined -- close, but not quite. Note: The message could have saved some time.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.