Iphone 变量在objective-c中的其他函数中变为null
我的ViewController中有两个变量。在我的Iphone 变量在objective-c中的其他函数中变为null,iphone,ios,objective-c,nsstring,Iphone,Ios,Objective C,Nsstring,我的ViewController中有两个变量。在我的.h中,我这样定义了它们 @property (nonatomic, strong) NSString *myLongitude; @property (nonatomic, strong) NSString *myLatitude; @synthesize myLatitude = _myLatitude; @synthesize myLongitude = _myLongitude; 在我的.m中,我合成了它们。像这样 @propert
.h
中,我这样定义了它们
@property (nonatomic, strong) NSString *myLongitude;
@property (nonatomic, strong) NSString *myLatitude;
@synthesize myLatitude = _myLatitude;
@synthesize myLongitude = _myLongitude;
在我的.m
中,我合成了它们。像这样
@property (nonatomic, strong) NSString *myLongitude;
@property (nonatomic, strong) NSString *myLatitude;
@synthesize myLatitude = _myLatitude;
@synthesize myLongitude = _myLongitude;
然后在函数updateLocation
中,我将这些变量设置如下
_myLongitude = [[NSString alloc]initWithFormat:@"%f",location.longitude];
_myLatitude = [[NSString alloc]initWithFormat:@"%f",location.latitude];
当我记录这些变量之后,我设置了它们,我得到了正确的值。但是,当我在更新函数之后执行的另一个函数中记录值时,这些变量变为NULL
。这是我的日志
NSLog(@"longitude val is = %@", _myLongitude);
NSLog(@"latitude val is = %@", _myLatitude);
有什么帮助吗
然后在函数updateLocation中,我将这些变量设置为
跟着
_myLongitude = [[NSString alloc]initWithFormat:@"%f",location.longitude];
_myLatitude = [[NSString alloc]initWithFormat:@"%f",location.latitude];
很明显,这些是类的属性,updateLocation
不是alloc
ate和init
序列化它们的正确位置
您应该在、viewDidLoad
、init
或类似的方法中执行此操作
如果要重新设置值,请将其设置为
nil
,然后放置新值,而不是执行此操作。如果使用ARC,则不需要像@synthesis myLatitude=\u myLatitude代码>用@合成我的语言就足够了代码>也可以为空,因为您的位置是,我使用ARC。这可能是问题所在吗?首先检查您的位置。lon/lat不为null在将其转换为字符串之前放入NSLog:)尝试一次,对这些变量使用setter,而不是使用_myLongitude或_myLatitude。在将它们传递到google API之前,将它们转换为字符串。因此我只需[[alloc]init]在ViewDidLoad中加载它们并在updateFunction中设置它们?我已经尝试过了,但没有成功。一直是空的。我在-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation函数中设置了变量。对象位置
必须释放,从而导致\u myLatitude
和\u MyLongitatudity
为空。它是strong类型吗?是的,它们是strong类型。您是否正在创建位置的不同实例?否则,我看不出任何问题:(