从Singleton获取字符串

时间:2013-07-06 11:31:18

标签: objective-c singleton

我有一个产生长,拉特的单身人士。我可以记录它们但希望能够得到它们。我做错了什么?

singleton.h

@interface ClassLocationManager : NSObject <CLLocationManagerDelegate> {

    NSString *lat;
    NSString *longt;
}

@property (nonatomic, strong) CLLocationManager* locationManager;
@property (nonatomic, retain) NSString *lat;
@property (nonatomic, retain) NSString *longt;

+ (ClassLocationManager*) sharedSingleton; 

singleton.m - long,lat字符串在 - (id)init

中给出值
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    int degrees = newLocation.coordinate.latitude;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
    NSLog(@" Current Latitude : %@",lat);

    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
    NSLog(@" Current Longitude : %@",longt);
    [manager stopUpdatingLocation];

}

codethatcallsthem.m

NSString *formatted3 = [[ClassLocationManager sharedSingleton] longt];
NSString *formatted4 = [[ClassLocationManager sharedSingleton] lat];

非常感谢。

1 个答案:

答案 0 :(得分:2)

您的房产与您的ivar名称不符..

  1. 让ivars离开并使用自动创建的(_longt,_lat)

  2. 或者更好的是说self.longt = and self.lat =

  3. OR bad:使用@synthesize使属性使用现有的ivars

  4. - 我会选择2。