【发布时间】:2013-02-28 09:02:41
【问题描述】:
我的 ViewController 中有两个变量。在我的.h 中,我是这样定义它们的。
@property (nonatomic, strong) NSString *myLongitude;
@property (nonatomic, strong) NSString *myLatitude;
在我的.m 中,我已经合成了它们。像这样。
@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);
有什么帮助吗?
【问题讨论】:
-
如果你使用 ARC,那么你不需要像
@synthesize myLatitude = _myLatitude;那样进行合成,@synthesize myLatitude;就足够了,因为你的位置也可以是 null -
是的,我使用 ARC。这可能是问题吗?
-
首先检查你的 location.lon/lat 不为空,把 NSLog 转换成字符串 :)
-
尝试一次,对这些变量使用 setter,而不是使用 _myLongitude 或 _myLatitude。
-
在将它们传递给 google API 之前将它们转换为字符串。
标签: iphone ios objective-c nsstring