Search code examples
iosuitableviewnsarray

Warning: Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray<NSString *> * _Nullable'


I'm trying to figure out how to get around this warning. I'm populating a UITableView with the contents of the docs directory. The code executes fine but I need to clean up this warning.

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

- (void)viewDidLoad {

[super viewDidLoad];

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

}

It's warning me: Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray * _Nullable'

If I change NSMutableArray to NSArray I get ARC Semantic Issue errors later in:

[tableData removeObjectAtIndex:indexPath.row];

and

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

it tells me: No visible @interface for 'NSArray' declares the selector 'removeObjectAtIndex:'

docPath jas to be a string and tableData has to be some sort of an array so I'm confused. Thanks!!


Solution

  • contentsOfDirectoryAtPath returns immutable NSArray. You have to make a mutableCopy

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

    or use an initializer

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