双打 - 分配,保留,读写?

时间:2011-07-25 09:06:20

标签: iphone objective-c ios memory-management

我正在将包含纬度/经度的XML文档解析为自定义DTO对象。当我尝试从double值设置标签时,我收到错误,但登录到控制台工作。我确信这是一个记忆问题。我有这段代码:

@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 *)的文本,但不设置坐标的文本。

2 个答案:

答案 0 :(得分:0)

使用界面构建器或

连接lblCords
lblCoords = [[UILabel alloc] init];

lblCoords.text = [NSString stringWithFormat:@"%@, %@", result.location.location.latitude, result.location.location.longitude];  
lblCoords.text = [NSString stringWithFormat:@"%f, %f", result.location.location.latitude, result.location.location.longitude];

答案 1 :(得分:0)

似乎lblCoords标签已被解除分配,如消息所示:

  

消息发送到解除分配的实例异常

尝试使用text的setter时。查看您管理此标签的方式。 double类型不是对象类型,并且不需要关心它们的内存管理(对于int等其他基类类型也是如此。)