0

I have a NSMUtableArray where I want to insert at different index and in different method some data. So I initialize my NSMutableArray in my viewDidLoad like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    params=[[NSMutableArray alloc]init];
    params =[NSMutableArray arrayWithCapacity:20];
    for (int i = 0; i<20; i++) {

        [params addObject:[NSNull null]];
    }

}

And in different method I try to replace the null value by the real value:

-(void)doInBackGround{

    NSString * domaine=@"Toto";
    int port =8080;

    [params replaceObjectAtIndex:8 withObject:domaine];

    [params replaceObjectAtIndex:9 withObject:[NSString stringWithFormat:@"%d",nbPort]];

}

Another method where I try to replace the value in the NSMutableArray "params"

-(void)someMethod{

    NSString * computer=@"Ajax";
    int port =3333;

    [params replaceObjectAtIndex:4 withObject:computer];

    [params replaceObjectAtIndex:5 withObject:[NSString stringWithFormat:@"%d",nbPort]];

}

But I have a crash in line where I try to replace the Object:

[params replaceObjectAtIndex:8 withObject:domaine];

How can I fix It?. I think that my problem is where I intialize the NSMUtableArray? What do you think?

4
  • 1
    What information do you get when it crashes? Commented Apr 25, 2013 at 7:32
  • 1
    with arc or withour arc? without arc this may crash because of autoreleased array Commented Apr 25, 2013 at 7:37
  • the information is "'NSInvalidArgumentException', reason: '-[NSConcreteMutableData replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x74938d0'" Commented Apr 25, 2013 at 7:40
  • I'm not using ARC architecture Commented Apr 25, 2013 at 7:52

2 Answers 2

2

First off, you're initializing params twice, the first one is entirely superfluous, as you're never doing anything with the empty array.

Your second initialization, using arrayWithCapacity: returns an autoreleased object, so by the time you're trying to replace objects in it, it has likely been deallocated.

Familiarize yourself with some memory management basics and use a retained property for the array. You might also want to switch to using ARC (automatic reference counting), which makes this kind of error less likely (though it's still helpful to know about memory management).

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

8 Comments

that's why I asked if we he is using arc
I know it's not mentioned in the question, but with those symptoms, I'd say it's quite unlikely that he's using ARC.
@omz why? It's on by default for new projects and has been for a long while.
Not sure what's the default because the templates seem to remember the last setting that was used, but anyway, this looks very much like a dangling pointer (which the last comment seems to confirm), and that doesn't happen easily with ARC.
Well, without having some objects in the array, he couldn't replace anything. Maybe the actual code that fills the array is more complex and not relevant here, so this is just used to demonstrate the problem...
|
0

First initialise your params ivar before you do any modification on it.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.loginValidated =NO;
    NSLog(@"Affichage Login Form");

  params = [[NSMutableArray arrayWithCapacity:20] retain]; 
   // params =[NSMutableArray arrayWithCapacity:20];
    for (int i = 0; i<20; i++) {

        [params addObject:[NSNull null]];
    }

    // Do any additional setup after loading the view from its nib.

   [self performSelectorInBackground:@selector(doInBackGround) withObject:nil];
}

And as an extra tip for your doInBackground so you can prevent unrecognised selector issues:

-(void)doInBackGround{

    NSString * domaine=@"Toto";
    int nbPort =8080;

    if(params){
       [params replaceObjectAtIndex:8 withObject:domaine];
       [params replaceObjectAtIndex:9 withObject:[NSString stringWithFormat:@"%d",nbPort]];
   }
}

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.