Search code examples
iosobjective-carrayscocoakvc

How to use with KVC accessors for a mutable array Objective-C


I'm trying to make a KVC compliant mutable array to detect changes in the array.

In my header file I have:

@interface ObjectDataModel : NSObject{
@private
    NSMutableArray *objectArray;
}

@property (nonatomic, strong) NSArray *objectArray;

- (NSArray *)objectArray;
- (NSUInteger)objectArrayCount;
- (id)objectInFilePathsArrayAtIndex:(NSUInteger)index;
- (id)objectArrayAtIndexes:(NSIndexSet *)indexes;
- (void)insertObject:(id)val inObjectArrayAtIndex:(NSUInteger)index;
- (void)insertObjectArray:(NSArray *)array atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectFromObjectArrayAtIndex:(NSUInteger)index;
- (void)removeObjectArrayAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInObjectArrayAtIndex:(NSUInteger)index withObject:(id)object;
- (void)replaceObjectArrayAtIndexes:(NSIndexSet *)indexes withObjectArray:(NSArray *)array;



@end

The typical accessor methods for KVC as shown on the Docs. When I implement the accessor methods in the main file, these following functions dont recognize the calls to the selectors:

@synthesize filePathsArray = _filePathsArray;

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

//
- (void)insertObject:(id)val inObjectArrayAtIndex:(NSUInteger)index
{
    [self.objectArray insertObject:val atIndex:index];
}

- (void)insertObjectArray:(NSArray *)array atIndexes:(NSIndexSet *)indexes{
    [self.objectArray insertObjects:array atIndexes:indexes];
}

- (void)removeObjectFromObjectArrayAtIndex:(NSUInteger)index{
    [self.objectArray removeObjectAtIndex:index];
}

- (void)removeObjectArrayAtIndexes:(NSIndexSet *)indexes{
    [self.objectArray removeObjectsAtIndexes:indexes];
}

- (void)replaceObjectInObjectArrayAtIndex:(NSUInteger)index withObject:(id)object{
    [self.objectArray replaceObjectAtIndex:index withObject:object];
}

- (void)replaceObjectArrayAtIndexes:(NSIndexSet *)indexes withObjectArray:(NSArray *)array{
    [self.objectArray replaceObjectsAtIndexes:indexes withObjects:array];
} 

The error is "no visible @interface for NSarray" declares selector:'' for each of the selectors in the function here. I am confused and unsure why these selectors do not exist? Help is appreciated. The other 4 functions in the header file are ok.


Solution

  • You have

    @property (nonatomic, strong) NSArray *objectArray;
    

    which is immutable. Use

    @property (nonatomic, strong) NSMutableArray *objectArray;
    

    instead