NSMutableArray重置自己

时间:2014-10-11 11:22:30

标签: ios objective-c arrays nsmutablearray

我在两个可变阵列上执行一些基本操作。但是,该程序表现得很奇怪。函数结束后,第二个数组将自行重置。

首先,我为两个数组分配内存。然后,我将originalArray填入0..10的数字。

我的代码看起来像这样。

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]];

}

输出:

Initial Array is (
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
)

Num: 0
Level: 1

SecondA: (
    0
)

Num:1
Level: 1

SecondA2: (
)

1 个答案:

答案 0 :(得分:1)

我复制并粘贴了您上面的代码,并从您显示的内容中获得不同的输出。你在设备上运行吗?也许该设备有旧版本的构建?

这是我得到的,secondArray看起来很好。正如计时器所期望的那样,最后三行记录每两秒重复一次。

输出中看起来很奇怪的其他内容是SecondA值的第一次记录缺失。

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
)