gpt4 book ai didi

ios - NSMutableArray 重置自身

转载 作者:行者123 更新时间:2023-11-29 02:36:27 24 4
gpt4 key购买 nike

我正在对两个可变数组执行一些基本操作。但是,该程序的行为很奇怪。第二个数组在函数结束后自行重置。

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

我的代码如下所示。

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: (
)

最佳答案

我在上面复制并粘贴了您的代码,但得到的输出与您显示的不同。你是在设备上运行这个吗?也许设备有旧版本的构建?

这是我得到的结果,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
)

关于ios - NSMutableArray 重置自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com