gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:37:14 25 4
gpt4 key购买 nike

我正在尝试制作一个兼容 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com