I have an object defined like this:
Scores.h:
@interface Scores : NSObject {
NSString *sentenceKey;
NSMutableArray *scorrectAnswers;
}
@property (nonatomic, copy) NSString *sentenceKey;
@property (nonatomic, copy) NSMutableArray *scorrectAnswers;
+ (id)addScore:(NSString *)senKey;
- (id)initWithSentenceKey:(NSString *)sKey
scorrectAnswers:(NSMutableArray *)scorrectAs;
- (id)initWithSentenceKey:(NSString *)sKey;
- (void)removeArrayObjects;
Score.m:
#import "Scores.h"
@implementation Scores
@synthesize sentenceKey, scorrectAnswers;
+ (id)addScore:(NSString *)senKey
{
Scores *newScore = [[self alloc] initWithSentenceKey:senKey
scorrectAnswers:[NSMutableArray new]];
return [newScore autorelease];}
I’m trying to removeAllObjects on my mutable array with this method:
- (void)removeArrayObjects;{
[scorrectAnswers removeAllObjects];}
…which I call from another program like this:
for (Scores *sScore in scores)
{
[sScore removeArrayObjects];
}
… and I get this error when I run it:
-[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x53412d0
Can anyone tell me what I’m doing wrong here? Thanks.
You are not dealing with an
NSMutableArrayas the error indicates you have an immutableNSArray.This question may be your answer NSMutableArray addObject: -[__NSArrayI addObject:]: unrecognized selector sent to instance
Basically the
copyyou used when defining your@propertywill cause the setter to be generated usingwhich returns an immutable
NSArray.You can re-implement this method and change the previous line for:
or use
retaininstead ofcopyThis can also occur when getting data from a
plistIf you are using a
plistit will return anNSArrayeven if you save anNSMutableArrayit will be cast. So when retrieving you will need to do something like: