开发者

Doubles - Assign, Retain, Readwrite?

开发者 https://www.devze.com 2023-03-22 12:11 出处:网络
I\'m parsing an XML document containing latitude/longitudes into custom DTO objects. 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

I'm parsing an XML document containing latitude/longitudes into custom DTO objects. 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 : NSObje开发者_如何学运维ct {
    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:

    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.


Connect lblCords using interface builder or

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 :

message sent to deallocated instance exception

while trying to use the setter for 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, ...).

0

精彩评论

暂无评论...
验证码 换一张
取 消