1

私はもともとスコープに問題があった可変配列を持っています。私はそれを修正する方法についていくつかのアドバイスを得ましたが(うまくいきました)、現在、配列は特定のインデックスのオブジェクトを置き換えません。

コードは次のとおりです。

.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

他にもいくつかありましたが、どれも機能しませんでした....

これが私の最初のアプリを完成させるまでの間に立っている唯一のものであるため、何か助けていただければ幸いです.

ありがとう!

4

3 に答える 3

3

First, class names should be capitalized. I say "first" because this indicates that you aren't following convention and that can lead to problems in comprehension, if not flat out bugs.

Secondly, are you sure the init method is being invoked at all? If your object is instantiated by the interface file load, then it likely isn't and the array is likely nil.

于 2012-04-11T20:25:32.703 に答える
0

指定された初期化子はUIViewControllerです-initWithNibName:bundle:。ではなく、それをオーバーライドする必要があります-init。あなた-initは呼び出されていないので、myArray;に何も割り当てていません。そのままですnil

于 2012-04-12T15:44:26.077 に答える
0

前に、現在のビュー メソッドの 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...");

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

できます !

于 2012-04-11T22:02:45.970 に答える