我决定尝试制作一个Singleton用于地点。我有我认为的,单身人士正常工作,但我有一个错误,现在出现。它告诉我,我的实施是不完整的。这是它给我的唯一错误,我确信我的视图标题或m文件有问题。我现在尝试了一些东西,却无法让它发挥作用。我在这里缺少什么?
这是我的标题
#import <UIKit/UIKit.h>
@interface TrackerViewController : UIViewController
-(void) locationUpdate;
end
这是我的实施文件:
#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
我的单身标题如下:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationControllerDelegate <NSObject>
@required
- (void)locationUpdate:(CLLocation*)location;
@end
@interface LocationController : NSObject <CLLocationManagerDelegate> {
__unsafe_unretained id <LocationControllerDelegate> _locDelegate;
CLLocationManager* locationManager;
CLLocation* location;
id locDelegate;
}
@property (nonatomic, retain) CLLocationManager* locationManager;
@property (nonatomic, retain) CLLocation* location;
@property (nonatomic, assign) id <LocationControllerDelegate> locDelegate;
+ (LocationController*)sharedInstance;
@end
我的单例实现如下:
#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
我在这里错过了什么或做错了什么?
答案 0 :(得分:2)
我认为问题的根源在于:
@protocol LocationControllerDelegate <NSObject>
@required
- (void)locationUpdate:(CLLocation*)location;
@end
所以你已经用一个必需的方法定义了一个协议,但除非我错过了你,否则你没有在采用该协议的对象中的任何地方实现该方法。编译器警告您尚未实现协议所需的所有方法。
答案 1 :(得分:0)
你的单身人士看错了。您的sharedInstance方法应为:
+ (LocationController*)sharedInstance {
@synchronized(self) {
if (sharedCLDelegate == nil) {
sharedCLDelegate = [[self alloc] init];
}
}
return sharedCLDelegate;
}
你可以摆脱allocWithZone:和copyWithZone:方法 - 它们没有做任何有用的事情。
这很可能是因为你的班级没有工作,但可能与“不完整的实施”警告无关。警告将是因为您忘记实现在标题或协议中声明的方法,或者您可能拼错了它。我无法在第一次通过时发现它,但我会再看看。
如果双击标题中的黄色警告图标,它应该告诉您哪种方法没有正确实施。
顺便说一句,下面这行是因为你是双重保留而泄漏(init设置保留计数为1,然后分配给保留属性将其设置为2)。您应该运行Analyze功能,这将为此类泄漏添加蓝色警告。
self.locationManager = [[CLLocationManager alloc] init];
请执行以下操作之一:
//assign directly to the ivar so the setter method isn't used
locationManager = [[CLLocationManager alloc] init];
或者
//autorelease before assigning
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
答案 2 :(得分:0)
locationUpdate
在您的界面中声明,但未在实现中定义。 locationUpdate:
已定义 - 关闭,但不完全。
注意:该消息可以节省一些时间。