Tuesday, 15 May 2012

ios - NSMutableArray resets itself -



ios - NSMutableArray resets itself -

i'm performing basic operations on 2 mutable arrays. however, programme behaves strangely. sec array resets after function ends.

first, allocate memory 2 arrays. populate originalarray numbers 0..10.

my code looks this.

viewcontroller.h

@property (nonatomic, strong) nsmutablearray *originalarray; @property (nonatomic, strong) nsmutablearray *secondarray;

viewcontroller.c

static int level=1; - (void)viewdidload { [super viewdidload]; [self firstarrayinit]; [self secondarrayinit]; [self startdisplaying]; } -(void) firstarrayinit{ self.originalarray = [nsmutablearray array]; (int i=0; i<10; i++) { [self.originalarray addobject:[nsnumber numberwithinteger:i]]; } } -(void)secondarrayinit{ self.secondarray = [[nsmutablearray alloc]init]; } -(void) startdisplaying{ nslog(@"initial array %@", self.originalarray); self.mytimer = [nstimer scheduledtimerwithtimeinterval:2 target:self selector:@selector(displaynumber) userinfo:nil repeats:yes]; nslog(@"seconda: %@", self.secondarray); //prints empty array!! } -(void) displaynumber{ static int num=0; nslog(@"num: %d", num); nslog(@"level: %d", level); if (num < level){ //display on label mylabel.text = [nsstring stringwithformat:@"%@", [self.originalarray objectatindex:num]]; [self addtosecondarray:num]; num++; nslog(@"seconda: %@", self.secondarray); //prints contents of array fine!! } else{ nslog(@"seconda2: %@", self.secondarray); //prints empty array!! } } -(void)addtosecondarray:(int)mynumber{ [self.secondarray addobject:[self.originalarray objectatindex:mynumber]]; }

outputs:

initial array ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ) num: 0 level: 1 seconda: ( 0 ) num:1 level: 1 seconda2: ( )

i copied , pasted code above , different output showing. running on device? perhaps device has old version of build?

here out getting , secondarray looks fine. expected timer, lastly 3 lines of logging repeat every 2 seconds.

something else seems odd in output first logging of seconda value missing.

2014-10-11 07:29:05.419 test[58826:1475246] initial array ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ) 2014-10-11 07:29:05.420 test[58826:1475246] seconda: ( ) 2014-10-11 07:29:07.420 test[58826:1475246] num: 0 2014-10-11 07:29:07.420 test[58826:1475246] level: 1 2014-10-11 07:29:07.420 test[58826:1475246] seconda: ( 0 ) 2014-10-11 07:29:09.420 test[58826:1475246] num: 1 2014-10-11 07:29:09.420 test[58826:1475246] level: 1 2014-10-11 07:29:09.420 test[58826:1475246] seconda2: ( 0 ) 2014-10-11 07:29:11.420 test[58826:1475246] num: 1 2014-10-11 07:29:11.420 test[58826:1475246] level: 1 2014-10-11 07:29:11.420 test[58826:1475246] seconda2: ( 0 ) 2014-10-11 07:29:13.419 test[58826:1475246] num: 1 2014-10-11 07:29:13.420 test[58826:1475246] level: 1 2014-10-11 07:29:13.420 test[58826:1475246] seconda2: ( 0 ) 2014-10-11 07:29:15.419 test[58826:1475246] num: 1 2014-10-11 07:29:15.420 test[58826:1475246] level: 1 2014-10-11 07:29:15.420 test[58826:1475246] seconda2: ( 0 ) 2014-10-11 07:29:17.419 test[58826:1475246] num: 1 2014-10-11 07:29:17.420 test[58826:1475246] level: 1 2014-10-11 07:29:17.420 test[58826:1475246] seconda2: ( 0 )

ios objective-c arrays nsmutablearray

No comments:

Post a Comment