Reputation: 528
I have created two folders in my Documents directory. I have a UITableView which shows the items in the folders.
Subfolder = @"/Sent";
It gives me a Threat 1: signs SIGABRT on the line:
NSString *last = [dataPath stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
I have two table views, this code below has been working fine until I added the 2 folders to the documents directory.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == _tblConnectedDevices){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
}
cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if ([filePathsArray count] == 0){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",subFolder]];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dataPath error:nil];
}
if ([filePathsArray count] > 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",subFolder]];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dataPath error:nil];
NSString *last = [dataPath stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
cell.textLabel.text = last2;
}
return cell;
}
}
When I open one folder with a file in it, it loads fine, after, if i open the folder with nothing in it, I get the below error. If i first open the folder with nothing in it, its fine, then open the folder with something in it, its fine, then when I go back to the empty folder, again the same error. What am I missing here?
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Upvotes: 0
Views: 758
Reputation: 90117
You reassign filePathsArray
in the middle of your method. You then access objects at an index without checking if it's inside the bounds of the array. That's most likely not correct.
if ([filePathsArray count] > 0) {
/* ... */
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dataPath error:nil];
NSString *last = [dataPath stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
The number of objects in filePathsArray can be less than indexPath.row after you have reassigned it.
Upvotes: 1