0

So I'm trying to change all the booleans in an NSMutableArray that I made which I followed from here.

i have:

@property (nonatomic, retain) NSMutableArray *isDinosaurTapped;

and synthesized:

@synthesize isDinosaurTapped;

and set up [EDITED to show isDinosaurTapped]:

NSMutableArray *newDinosaurTaps = [[NSMutableArray alloc] init];
    for( int i = 0; i < [dinoSprites count]; i++ )
    {
        NSNumber *isTapped = [posPlist valueForKeyPath:[NSString stringWithFormat:@"Dinosaurs.Dinosaur_%i.isTapped", i]];
        [newDinosaurTaps addObject:isTapped];
    }
    self.isDinosaurTapped = [newDinosaurTaps copy];


    for( int i = 0; i < [isDinosaurTapped count]; i++ )
    {
        [isDinosaurTapped replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
    }

When I build its fine, however when I actually build and run, I keep getting a SIGABRT: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance.

Have I set the properties of the NSMutableArray incorrectly? But according to this, my properties should be ok.

Any feedback is greatly appreciated! :D

2
  • show some lines of code where you set isDinosaurTapped. ie self.isDinosaurTapped = ... or isDinosaurTapped = ... I think it is being set to an instance of NSArray Commented Mar 8, 2012 at 3:39
  • Why doesn't the compiler complain about you setting this to NSArray? It should show a yellow warning somewhere about incompatible types. Commented Mar 8, 2012 at 3:47

2 Answers 2

5

You are trying to mutate an immutable array. Is it declared as NSMutableArray, or NSArray, and if it is mutable, did you call -copy, or -mutableCopy?

In core foundation, NSMutableArrays are internally known as __NSArrayM (for mutable), and NSArrays are known as __NSArrayI (for immutable).

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

2 Comments

copy and mutableCopy are instance methods and should be prefixed with -, not +.
Thank you for your input! I did have "copy" instead of "mutablecopy". Didn't know it would change it when using just "copy". Thank you again! Solved my problem!
0

It complains that you are sending the replaceObjectAtIndex:withObject: selector to an NSArray which is immutable/unmodifiable after creation. NSMutableArray is the mutable version of NSArray. Did you initialize the isDinosaurTapped property with an NSArray? You should be calling something like this before you use your property.

self.isDinosaurTapped = [NSMutableArray array]; // or another nsmutable creation method.

If you have an NSArray that you want to clone into your property you can use:

self.isDinosaurTapped = [NSMutableArray arrayWithArray:array];

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.