如何使用KVC访问器获取可变数组Objective-C

时间:2013-09-25 14:58:35

标签: ios objective-c arrays cocoa kvc

我正在尝试制作符合KVC标准的可变阵列来检测阵列中的变化。

在我的头文件中,我有:

@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

文档中显示的KVC的典型存取方法。当我在主文件中实现访问器方法时,以下这些函数不会识别对选择器的调用:

@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];
} 

错误是“没有可见的@interface for NSarray”声明选择器:''对于此处函数中的每个选择器。我很困惑,也不确定为什么这些选择器不存在? 感谢帮助。头文件中的其他4个函数都可以。

1 个答案:

答案 0 :(得分:1)

你有

@property (nonatomic, strong) NSArray *objectArray;

这是不可变的。使用

@property (nonatomic, strong) NSMutableArray *objectArray;

代替