内存管理和类成员变量

时间:2011-04-11 22:02:08

标签: iphone objective-c

我有以下代码:

  @interface Reservation : RKObject {
    NSNumber * _uid;
    NSNumber * _did;
    NSNumber * _rid;
    NSString* _origin;
    NSString* _destination;
    NSString* _time_range;
    NSDate * _start_date;
    NSDate * _end_date;
}

@property (nonatomic, retain) NSDate * start_date;
@property (nonatomic, retain) NSDate * end_date;
@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSNumber * did;
@property (nonatomic, retain) NSNumber * rid;
@property (nonatomic, retain) NSString * time_range;
@property (nonatomic, retain) NSString * origin;
@property (nonatomic, retain) NSString * destination;
@end

#import "Reservation.h"

@implementation Reservation
@synthesize time_range= _time_range;
@synthesize origin=_origin;
@synthesize destination=_destination;
@synthesize uid=_uid;
@synthesize did=_did;
@synthesize rid=_rid;
@synthesize start_date=_start_date;
@synthesize end_date=_end_date;


+ (NSDictionary*) elementToPropertyMappings {
    return [NSDictionary dictionaryWithKeysAndObjects:
            @"TIME_RANGE", @"time_range",
            @"ORIGIN_ADDRESS", @"origin",
            @"DESTINATION_ADDRESS", @"destination",
            @"UID", @"uid",
            @"DID", @"did",
            @"RID", @"rid",
            @"START_DATETIME", @"start_date",
            @"END_DATETIME", @"end_date",
            nil]; 
}


/*
+ (NSDictionary*)elementToRelationshipMappings {
    return [NSDictionary dictionaryWithKeysAndObjects:
            @"NSDate", @"start_date",
            @"NSDate", @"end_date",
            nil];
}
 */

- (void)dealloc
{
    [_start_date release];
    [_end_date release];
    [_uid release];
    [_did release];
    [_rid release];
    [_origin release];
    [_destination release];
    [_time_range release];
    //[_deal release];
    //[_route release];
    [super dealloc];
}

@end

@interface ReservationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate>{
    Reservation * myres;
}

@property (nonatomic, retain) Reservation * myres;

#import "ReservationViewController.h"


@implementation ReservationViewController
@synthesize myres;

- (IBAction)makeReservation:(id)sender
{
    self.myres = [[[Reservation alloc] init] autorelease];  
    myres.start_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]];
    myres.end_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]];
    NSLog(@"date is %@", myres.start_date);
}

- (void)objectLoader: (RKObjectLoader*) objectLoader didLoadObjects:(NSArray *)objects {
        Interval * res_int = [[Interval alloc] init];
        res_int.date1 = myres.start_date; //why is it null here
        res_int.date2 = myres.end_date; //why is it null here
        res_int.rid =  [[objects objectAtIndex:0] rid];
        [[LocationDictionary sharedLocationDictionary] addLocationtoDictionary:[self.data objectForKey:@"route"] withKey:res_int];

}

问题是为什么当我尝试将其打印出来时res_int.date1为空?当我在方法makeReservation中打印myres.start_date时,它打印得恰当可以有人向我解释这个以及如何解决它吗?

更新

我在我的二传手做了以下事情:

-(void) setStart_date:(NSDate *) boo
{
     NSAssert(boo != nil, @"why is this nil");
    _start_date = boo;    

}

最后我得到的堆栈跟踪是:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'why is this nil'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x019ac5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01b00313 objc_exception_throw + 44
    2   CoreFoundation                      0x01964ef8 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x014013bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   Project                            0x00012d42 -[Reservation setStart_date:] + 194
    5   Foundation                          0x0137e5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    6   Project                           0x0004c7b3 -[RKObjectMapper updateModel:ifNewPropertyValue:forPropertyNamed:] + 172
    7   Project                            0x0004cd55 -[RKObjectMapper setPropertiesOfModel:fromElements:] + 754
    8   Project                            0x0004d8f3 -[RKObjectMapper updateModel:fromElements:] + 50
    9   Project                            0x0004bb7a -[RKObjectMapper mapObject:fromDictionary:] + 201
    10  Project                            0x0004ba42 -[RKObjectMapper mapObject:fromString:] + 148
    11  Project                            0x0004927b -[RKObjectLoader processLoadModelsInBackground:] + 537
    12  Foundation                          0x01370cf4 -[NSThread main] + 81
    13  Foundation                          0x01370c80 __NSThread__main__ + 1387
    14  libSystem.B.dylib                   0x931a085d _pthread_start + 345
    15  libSystem.B.dylib                   0x931a06e2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'

3 个答案:

答案 0 :(得分:1)

makeReservation(来自IBAction)和objectLoader的调用序列是什么?是否有可能在objectLoader之前调用makeReservation,因此myres尚未初始化?

预订的did属性的接口定义是什么?你的行:

myres.did = [[NSNumber alloc] initWithInt:[[[data objectForKey:@"discount"] did] intValue]];

如果定义为(nonatomic, retain),您可能会保留NSNumber对象而不释放它。如果是,请将行更改为:

myres.did = [NSNumber numberWithInt:[[[data objectForKey:@"discount"] did] intValue]];

您还应该取消注释:

//[res_int release]

答案 1 :(得分:0)

在makeReservation方法中,尝试以下操作:

self.myres = [[[Reservation alloc] init] autorelease];

这将保留Reservation对象。

编辑:XJones是对的。编辑代码行。

答案 2 :(得分:0)

您的问题可能发生在Reservation.h

确保您的日期属性声明如下:

@property (non-atomic, retain) NSDate *start_date;
@property (non-atomic, retain) NSDate *end_date;

此处的导入属性为保留

相关问题