0

I'm performing some basic operations on two mutable arrays. However, the program behaves strangely. The second array resets itself after a function ends.

First, I allocate memory for the two arrays. I then populate originalArray with numbers from 0..10.

My code looks like 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];
    for (int i=0; i<10; i++) {
        [self.originalArray addObject:[NSNumber numberWithInteger:i]];
    }

}

-(void)secondArrayInit{
    self.secondArray = [[NSMutableArray alloc]init];

}

-(void) startDisplaying{ 
        NSLog(@"Initial Array is %@", 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 a label
        myLabel.text = [NSString stringWithFormat:@"%@", [self.originalArray objectAtIndex:num]];
        [self addToSecondArray:num];
        num++;
        NSLog(@"SecondA: %@", self.secondArray); //prints the 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 is (
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
)

Num: 0
Level: 1

SecondA: (
    0
)

Num:1
Level: 1

SecondA2: (
)
user2771150
  • 722
  • 4
  • 10
  • 33
  • This code is completely useless alone. Publish the whole code with the call to `startDisplaying`, `viewDidLoad`, `viewWill/DidAppear`, etc. – Matteo Gobbi Oct 11 '14 at 11:29
  • Show us the declarations. – Hot Licks Oct 11 '14 at 11:55
  • It would be empty in `startDisplaying` because `displayNumber` hasn't been called yet. – Hot Licks Oct 11 '14 at 11:58
  • 1
    I guess your problem is that `SecondA2` should not be empty? What is the value of `level`? What is the output of the app (the order the output occurs is also important)? – fluidsonic Oct 11 '14 at 11:59
  • It would be empty in `displayNumber` if `level` is zero. – Hot Licks Oct 11 '14 at 11:59
  • I setup a test project with your code. Its working and the second array never becomes empty. I think you should provide your full code. – Tuan Oct 11 '14 at 12:05
  • It was working fine before the new Xcode update. I'm using Xcode 6.0.1. – user2771150 Oct 11 '14 at 12:13
  • You are accessing the `NSMutableArray` from multiple threads and `NSMutableArray` is not thread-safe. – Paulw11 Oct 11 '14 at 12:31
  • See here to add breakpoints to see when your variable is changed: http://stackoverflow.com/questions/18150221/how-do-i-have-a-breakpoint-get-triggered-if-an-instance-variable-in-the-class-ha – Tuan Oct 11 '14 at 18:11
  • Besides i reproduced your code and it runs. Error has to come from somewhere else which you did not provided here. – Tuan Oct 11 '14 at 18:13

1 Answers1

1

I copied and pasted your code above and get different output from what you are showing. Are you running this on a device? Perhaps the device has an old version of the build?

Here is the out I am getting and the secondArray looks fine. As expected by the timer, the last three lines of logging repeat every two seconds.

Something else that seems odd in your output is that the very first logging of the SecondA value is missing.

2014-10-11 07:29:05.419 test[58826:1475246] Initial Array is (
    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
)
picciano
  • 22,341
  • 9
  • 69
  • 82