ios - 警告 : Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray<NSString *> * _Nullable'

标签 ios uitableview nsarray

我正在尝试弄清楚如何绕过此警告。我正在使用 docs 目录的内容填充 UITableView。代码执行正常,但我需要清除此警告。

@implementation ViewSavedFiles
{
    NSMutableArray *tableData;
}
@synthesize myFileName;

- (void)viewDidLoad {

[super viewDidLoad];

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    tableData = [[NSFileManager defaultManager]  contentsOfDirectoryAtPath:docPath error:NULL];

}

它警告我:从 'NSArray * _Nullable' 分配给 'NSMutableArray *' 的指针类型不兼容

如果我将 NSMutableArray 更改为 NSArray,我稍后会收到 ARC 语义问题错误:

[tableData removeObjectAtIndex:indexPath.row];

[tableData insertObject:objectToMove atIndex:toIndexPath.row];

它告诉我: “NSArray”没有可见的@interface 声明选择器“removeObjectAtIndex:”

docPath jas 是一个字符串,而 tableData 必须是某种数组,所以我很困惑。 谢谢!!

最佳答案

contentsOfDirectoryAtPath 返回不可变的NSArray。你必须制作一个 mutableCopy

tableData = [[[NSFileManager defaultManager]  contentsOfDirectoryAtPath:docPath error:NULL] mutableCopy];

或者使用初始化器

NSArray *fileNames = [[NSFileManager defaultManager]  contentsOfDirectoryAtPath:docPath error:NULL];
tableData = [NSMutableArray arrayWithArray: fileNames];

关于ios - 警告 : Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray<NSString *> * _Nullable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51232975/

相关文章:

iphone - 比较两个自定义单元格中 UILables 的值

ios - 是否可以在 iOS 中调整相机预览的大小?

iphone - 当用户同时旋转组件时,UIPickerView 会导致应用程序崩溃

ios - 在 xcode 中配置时出错

iphone - NSMutableArray 与 NSMutableDictionary 的一个奇怪问题

iphone - 从 parse.com 获取 objectId

ios - NSDictionary 的 NSArray 上的 NSPredicate

ios - Swift 异常堆栈中的 Dead & Exploded 是什么?

ios - JSON 数据未加载到 UITableView 中

ios - 最初隐藏 UITableView 的 UISearchDisplayController(iOS SDK)?