Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a mutable array that, originally, I was having trouble with the scope. I got some advice on how to fix that (which worked), but now the array will not replace the objects at a given index.

Here is the code:

.h file:

@interface myViewController: UIViewController {
  NSMutableArray *myArray;
}

.m file:

-(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
}

I've tried other ways to insert, such as:

replaceObjectAtIndex: withObject

And some others, but none of them worked....

I would greatly appreciate any help as this is the only thing standing between me and finishing my first app.

Thanks!

share|improve this question
1  
It seems correct to me and actually works –  WhiteTiger Apr 11 '12 at 19:54
1  
how to initialize the class? is not that they use - init? trying to debug –  WhiteTiger Apr 11 '12 at 19:57
1  
ARC is a very important concept to at least know enough about to know whether or not you are using it. The short version (to answer the question) though is: When you created the new project, did you click the box that said "Use Automatic Reference Counting?" –  lnafziger Apr 11 '12 at 20:02
1  
How are you determining what works and what doesn't? –  Ken Thomases Apr 11 '12 at 20:27
1  
Well, if the value is nil then comparing it to 0 will still show as true.... –  lnafziger Apr 11 '12 at 20:39
show 9 more comments

3 Answers

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.

share|improve this answer
 
I just started on Objective-C, so I am a little lost in what you said..... –  user1165664 Apr 11 '12 at 20:39
 
Your init method is likely not being called (try putting an NSLog(@"Hello"); in it) and, thus, the instance variable myArray is nil. Or put NSLog(@"%@", myArray); in your viewDidLoad method. –  bbum Apr 11 '12 at 21:33
 
@user1165664 what bbum is trying to say is, don't put myArray = [[NSMutableArray alloc] init]; in the init method. Instead, put it in the viewDidLoad –  Jimmy Luong Apr 11 '12 at 23:04
 
Yeah... that. That'll work only if myArray is never used before viewDidLoad is called. –  bbum Apr 11 '12 at 23:50
2  
Is the vc loaded from a nib? You could do this in awakeFromNib... –  samson Apr 12 '12 at 17:40
add comment

Make sure you call the addSubview of your current view methode before.

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

[self.view addSubview:youViewNewController.view]; // Calls viewDidLoad.

this work for me,

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

it works !

share|improve this answer
add comment

The designated initializer for UIViewController is -initWithNibName:bundle:. You should override that rather than -init. Your -init is not getting called, so you are never assigning anything to myArray; it just remains nil.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.