1

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!

14
  • 1
    It seems correct to me and actually works Commented Apr 11, 2012 at 19:54
  • 1
    how to initialize the class? is not that they use - init? trying to debug Commented Apr 11, 2012 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?" Commented Apr 11, 2012 at 20:02
  • 1
    How are you determining what works and what doesn't? Commented Apr 11, 2012 at 20:27
  • 1
    Well, if the value is nil then comparing it to 0 will still show as true.... Commented Apr 11, 2012 at 20:39

3 Answers 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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@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
Yeah... that. That'll work only if myArray is never used before viewDidLoad is called.
0

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 !

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.