【问题标题】:Doubles - Assign, Retain, Readwrite?双打 - 分配,保留,读写?
【发布时间】: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


    【解决方案1】:

    使用接口构建器连接 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];
    

    【讨论】:

    • 标签在IB中连接。如果我这样做,我可以设置文本lblCoords.text = @"blah";
    【解决方案2】:

    如消息所述,lblCoords 标签似乎已被释放:

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

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

    【讨论】:

    • 它与界面生成器连接,我可以将文本设置为普通的NSString。
    • 属性retain 是标签吗?当您尝试访问它时,它会被释放
    • 是的,声明是@property (nonatomic, retain) IBOutlet UILabel *lblCoords;。我可以毫无问题地访问它,并且可以将它的文本设置为预编译的字符串。
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2021-11-12
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多