0

I'm trying to use the singleton concept to retain GPS information, but I do no see where is the problem my variable display just zeroes.

Singleton.h:

@interface Singleton : NSObject

@property (readwrite) NSString *propertyA;
@property (readwrite) NSString *propertyB;
@property (readwrite) NSString *propertyC;

+ (Singleton *)sharedInstance;

@end

Singleton.m:

#import "Singleton.h"

@interface Singleton ()
{
    NSString *variableA;
    NSString *variableB;
    NSString *variableC;
}
@end

@implementation Singleton

static Singleton *sharedInstance = nil;

+ (Singleton *)sharedInstance
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[Singleton alloc] init];
        }
    }
    return sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [super allocWithZone:zone];
            return sharedInstance;
        }
    }
    return nil;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        variableA = nil;
        variableB = nil;
        variableC = nil;
    }
    return self;
}

@end

in OverlayViewController.m:

#import "Singleton.h"

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
        NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;

        if (currentLocation != nil) {
            _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }

        // Setting properties
        NSString *variableA = _latitudeLabel.text;
        [[Singleton sharedInstance] setPropertyA:variableA];      
}

Then I recall the value a bit further down ...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     NSString *variableA = [[Singleton sharedInstance] propertyA];    
     NSLog(@"Latitude = %.8f", variableA);
}

Thanks Regis

4

2 回答 2

0

@property (readwrite) NSString *propertyA;
@property (readwrite) NSString *propertyB;
@property (readwrite) NSString *propertyC;

我猜上面的变量没有setter和getter,这意味着你需要@synthesize它们。

这就是为什么下面的代码没有设置 PropertyA 值的原因。

[[Singleton sharedInstance] setPropertyA:variableA];

于 2013-01-24T14:57:12.207 回答
0

我的第一个猜测是您在任何地方都没有任何@synthesize,所以没有设置器/获取器

如果这不起作用,那么你应该将你的变量声明移动到你的 .h 文件中,我不明白你为什么要把它们放在你的 .m 上

我指的是:

@interface Singleton ()
{
    NSString *variableA;
    NSString *variableB;
    NSString *variableC;
}
@end

编辑: 处理单例时的两个好习惯:

1)在您的方法中声明您的static ClassName *singleton内部,以便您的类中的其他方法无法直接访问它并且需要使用它来请求它[[self class] sharedInstance];

2) 使用 dispatch_once 来保证线程安全

+ (FooBar *)sharedInstance {
    static dispatch_once_t pred;
    static FooBar *_sharedInstance = nil;

    dispatch_once(&pred, ^{
        _sharedInstance = [[[self class] alloc] init];
    });

    return _sharedInstance;
}
于 2013-01-24T14:53:23.413 回答