【发布时间】:2011-07-25 09:06:19
【问题描述】:
我正在将包含纬度/经度的 XML 文档解析为自定义 DTO 对象。当我尝试从双精度值设置标签时出现错误,但登录到控制台有效。我确信这是一个内存问题。我有这个代码:
@interface LocationResult : NSObject {
LoginResultType result;
LocationInfo *location;
}
@property (nonatomic, readwrite) LoginResultType result;
@property (nonatomic, retain) LocationInfo *location;
@end
@interface LocationInfo : NSObject {
LatLng *location;
NSString *niceLocation;
}
@property (nonatomic, retain) LatLng *location;
@property (nonatomic, retain) NSString *niceLocation;
-(LocationInfo *)initWithLocation:(NSString *)strNiceLocation latitudeIs:(double)latitude longitudeIs:(double)lonitude withPostCode:(NSString *)postCode;
@end
@interface LatLng : NSObject {
NSString *postCode;
double latitude;
double longitude;
}
@property (nonatomic, retain) NSString *postCode;
@property (nonatomic, readwrite) double latitude;
@property (nonatomic, readwrite) double longitude;
-(LatLng*)initWithLocation:(NSString *)strPostCode latitudeIs:(double)latitude longitudeIs:(double)longitude;
@end
为了初始化对象,我使用 TouchXML 从 XML 文档中解析:
NSString *postCode =[[eleData nodeForXPath:@"Location/PostCode" error:nil] stringValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGeneratesDecimalNumbers:TRUE];
NSString *rawLat =[ [eleData nodeForXPath:@"Location/Latitude" error:nil] stringValue];
double lat = [rawLat doubleValue];
NSString *rawLng = [[eleData nodeForXPath:@"Location/Longitude" error:nil] stringValue];
double lng = [rawLng doubleValue];
[info initWithLocation:prettyLocation latitudeIs:lat longitudeIs:lng withPostCode:postCode];
NSLog([NSString stringWithFormat:@"%f", lat]); // works
NSLog(info.location.postCode); // works
NSLog([NSString stringWithFormat:@"%f", info.location.latitude]); // works
显示数据:
lblCurrentPostcode.text = result.location.location.postCode;
NSLog([NSString stringWithFormat:@"%d, %d", result.location.location.latitude, result.location.location.longitude]); // this works
lblCoords.text = [NSString stringWithFormat:@"%@, %@", result.location.location.latitude, result.location.location.longitude]; // message sent to deallocated instance exception, crashes app
lblCoords.text = [NSString stringWithFormat:@"%f, %f", result.location.location.latitude, result.location.location.longitude]; // message sent to deallocated instance exception, crashes app
我不明白为什么我可以登录到控制台并设置 PostCode 的文本(一个 NSString*),但不能设置坐标的文本。
【问题讨论】:
标签: iphone objective-c ios memory-management