Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSMutableArray replaceObjectAtIndex:withObject:不工作_Iphone_Objective C_Ios_Arrays_Nsmutablearray - Fatal编程技术网

Iphone NSMutableArray replaceObjectAtIndex:withObject:不工作

Iphone NSMutableArray replaceObjectAtIndex:withObject:不工作,iphone,objective-c,ios,arrays,nsmutablearray,Iphone,Objective C,Ios,Arrays,Nsmutablearray,我有一个可变数组,最初,我在作用域上遇到了问题。我得到了一些关于如何修复该问题的建议(这是有效的),但现在数组将不会替换给定索引处的对象 代码如下: .h文件: @interface myViewController: UIViewController { NSMutableArray *myArray; } .m文件: -(id)init { self = [super init]; if (self){ myArray = [[NSMutableArray alloc]

我有一个可变数组,最初,我在作用域上遇到了问题。我得到了一些关于如何修复该问题的建议(这是有效的),但现在数组将不会替换给定索引处的对象

代码如下:

.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
}
还有其他一些,但都不起作用

我将非常感谢任何帮助,因为这是唯一一件事站在我和完成我的第一个应用程序


谢谢

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


第二,您确定正在调用
init
方法吗?如果您的对象是通过接口文件加载实例化的,那么它可能不是,数组可能为零。

请确保在调用当前视图方法的addSubview之前

YouViewNewController *youViewNewController=[[YouViewNewController alloc]init];

[self.view addSubview:youViewNewController.view]; // Calls viewDidLoad.
这是我的工作

-(id)init {
self = [super init];
if (self){
    myArray = [[NSMutableArray alloc] init];
    NSLog(@"Lancement...");
}
return self;
}

-(void)viewDidLoad
{
NSLog(@“Calcul…”);

对于(int x=0;xUIViewController
的指定初始值设定项是
-initWithNibName:bundle:
。您应该覆盖它,而不是
-init
。您的
-init
没有被调用,因此您永远不会将任何内容分配给
myArray
;它只是保持为
nil
,在我看来这是正确的,并采取行动如何初始化类?不是因为他们使用了-init吗?尝试debugARC是一个非常重要的概念,至少要知道您是否在使用它。简短的版本(回答问题)是:当您创建新项目时,是否单击了“使用自动引用计数”的框您如何确定哪些有效,哪些无效?如果值为nil,那么将其与0进行比较仍将显示为true…您的
init
方法可能未被调用(尝试在其中放入
NSLog(@“Hello”);
),因此,实例变量
myArray
nil
,或者put
NSLog(@“%@”,myArray);
在您的
viewDidLoad
方法中。@user1165664 bbum想说的是,不要将
myArray=[[NSMutableArray alloc]init];
init
方法中。取而代之的是,将其放入
viewDidLoad
中。只有在调用
viewDidLoad
之前从未使用过
myArray
时,这才有效。
-(id)init {
self = [super init];
if (self){
    myArray = [[NSMutableArray alloc] init];
    NSLog(@"Lancement...");
}
return self;
-(void)viewDidLoad 
{
   NSLog(@"Calcul...");

   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

    NSLog(@"%i", [[myArray objectAtIndex:0]intValue]);
}