简体   繁体   English

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

[英]Doubles - Assign, Retain, Readwrite?

I'm parsing an XML document containing latitude/longitudes into custom DTO objects. 我正在将包含纬度/经度的XML文档解析为自定义DTO对象。 When I try to set a label from a double value I get an error, yet logging to the console works. 当我尝试从双精度值设置标签时,出现错误,但登录到控制台仍然有效。 I'm convinced this is a memory issue. 我相信这是一个内存问题。 I have this code: 我有以下代码:

@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

To initialize the object, I'm parsing from an XML doc using TouchXML: 为了初始化对象,我使用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

To display the data: 要显示数据:

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

I don't understand why I can log to the console and set the text of PostCode (an NSString*), but not set the text for the coordinates. 我不明白为什么我可以登录控制台并设置PostCode(NSString *)的文本,但不能设置坐标的文本。

Connect lblCords using interface builder or 使用界面生成器连接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];

It seems that lblCoords label has been deallocated, as the message states : 似乎该lblCoords已声明lblCoords标签已被释放:

message sent to deallocated instance exception 消息发送到已释放实例异常

while trying to use the setter for text . 尝试使用设置器进行text Check out the way you manage this label. 查看管理此标签的方式。 double type isn't object type, and there is no need to care about memory managment for them (this is also true for other base types like int , ...). double类型不是对象类型,也不需要关心它们的内存管理(对于其他基本类型(如int ,...)也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM