ios - 如何使用 KVC 访问器来访问可变数组 Objective-C

标签 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];
} 

错误是“novisible @interface for NSarray”为函数中的每个选择器声明了选择器:''。我很困惑并且不确定为什么这些选择器不存在? 感谢帮助。头文件中的其他4个函数都可以。

最佳答案

你有

@property (nonatomic, strong) NSArray *objectArray;

这是不可变的。使用

@property (nonatomic, strong) NSMutableArray *objectArray;

相反

关于ios - 如何使用 KVC 访问器来访问可变数组 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008409/

相关文章:

objective-c - 为什么 UIPopoverPresentationController 内的 View Controller 占据整个屏幕?

ios - 删除滑动时调整 UITableViewCell 高度

arrays - 将列组合成嵌套数组

ios - UILocalNotifcation 自定义触发事件,或者与 NSTimer 斗争

ios - 相当于#pragma poison 的警告

iphone - Cocos2D图像如何反复缩小

ios - 如何使用 Storyboard/IB 从自定义 View 操作中推送 View Controller ?

ios - didFinishPickingMediaWithInfo 方向问题

python - 使用连续数字将列表向左和向右扩展至总长度的 50%

javascript - 用 json 数据填充现有数组