gpt4 book ai didi

iphone - NSMutableArray replaceObjectAtIndex : withObject: Not Working

转载 作者:行者123 更新时间:2023-12-01 19:21:43 27 4
gpt4 key购买 nike

我有一个可变数组,最初我在使用范围时遇到了问题。我得到了一些关于如何解决这个问题的建议(这很有效),但现在数组不会替换给定索引处的对象。

这是代码:

.h 文件:

@interface myViewController: UIViewController {
NSMutableArray *myArray;
}

.m 文件:
-(id)init {
self = [super init];
if (self){
myArray = [[NSMutableArray alloc] init];
}
return self;
}

-(void)viewDidLoad {
for (int x=0; x<6; x++) {
[myArray addObject:[NSNumber numberWithInt:0]]; //This adds the numbers just fine
}
[myArray insertObject:[NSNumber numberWithInt:1] atIndex:0]; //This does not insert the number 1 into index 0
}

我尝试了其他插入方式,例如:
replaceObjectAtIndex: withObject

还有一些其他的,但没有一个起作用....

我将不胜感激任何帮助,因为这是我和完成我的第一个应用程序之间唯一的事情。

谢谢!

最佳答案

首先,类名应该大写。我说“第一”是因为这表明您没有遵循约定,如果不是完全错误的话,可能会导致理解上的问题。

其次,你确定init方法正在被调用吗?如果您的对象是由接口(interface)文件加载实例化的,那么它可能不是并且数组可能为零。

关于iphone - NSMutableArray replaceObjectAtIndex : withObject: Not Working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112918/

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