0
votes

Working on a simple project and my NSMutableArrays won’t insert and object when using a property from a class to talk to the array. So I went to a command line in Xcode and any arrays created in main work from main then if I use .notation to talk to the array property I get the error unrecognized selector. I looked up some things an tried using a method to add objects like - (void)addObjectsToArray {

MyClass *myObject = [[MyClass alloc] init];
[list addObject:myObject];
[myObject release];

OtherClass *anotherObject = [[OtherClass alloc] init];
[list addObject:anotherObject];
[anotherObject release];

}

but ANY time I ask my array to addObject OR insertObject I get the unrecognized selector error (that is when ever I try using self.array or any .array from anywhere)

Here is my class .h there is no code just the property. I am trying to use newObject.array and then call insertObject on it but no go…

import

@interface CheckArray : NSObject

@property (copy, nonatomic) NSMutableArray *array;

@end

and in main I have

CheckArray *newObject = [[CheckArray alloc]init];

    newObject.array  = [[NSMutableArray alloc]init];

    if (newObject.array) {
        [newObject.array insertObject:testString atIndex:0];
    }

    NSLog(@" the new array is %@“, newObject.array);

the log says 2015-01-19 00:14:32.940 TestOfMutableArray[16647:518334] new array is ( ) so it exists but ....

here’s the error why does it not recognize insertObject? Thanks if you can explain.

2015-01-19 00:05:59.884 TestOfMutableArray[16608:513530] -[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x1001068d0 2015-01-19 00:05:59.885 TestOfMutableArray[16608:513530] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x1001068d0'

2

2 Answers

0
votes

__NSArrayI means immutable array, immutable arrays doesn't declares method for mutating its contents. Somewhere you pass to .array an NSArray not a NSMutableArray

0
votes

Because you are using copy to declare your property, it will turn it into immutable which is NSArray. Try to modify the copy to strong.

For your case, you are using a NSMutableArray pointer to reference a NSArray. That's why you can not insert.