iphone - memory management and class member variable -
i have following code:
@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 %@", myres.start_date); } - (void)objectloader: (rkobjectloader*) objectloader didloadobjects:(nsarray *)objects { interval * res_int = [[interval alloc] init]; res_int.date1 = myres.start_date; //why null here res_int.date2 = myres.end_date; //why null here res_int.rid = [[objects objectatindex:0] rid]; [[locationdictionary sharedlocationdictionary] addlocationtodictionary:[self.data objectforkey:@"route"] withkey:res_int]; }
the question why res_int.date1 null when tried printing out? when print myres.start_date inside method makereservation, prints appropriately can explain me , how fix it?
update:
i have done following @ setter:
-(void) setstart_date:(nsdate *) boo { nsassert(boo != nil, @"why nil"); _start_date = boo; } at end stack trace got is:
*** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'why nil' *** call stack @ 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 instance of 'nsexception'
what calling sequence of makereservation (from ibaction) , objectloader? possible objectloader called before makereservation myres hasn't been initialized yet?
what interface definition did property of reservation? line:
myres.did = [[nsnumber alloc] initwithint:[[[data objectforkey:@"discount"] did] intvalue]]; if defined (nonatomic, retain) may retaining nsnumber object without releasing it. if so, change line to:
myres.did = [nsnumber numberwithint:[[[data objectforkey:@"discount"] did] intvalue]]; you should uncomment line:
//[res_int release]
Comments
Post a Comment