Question

I try to remove the file but when I use "indexPath.row" to get the file name it sends me always 0...

This is my code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSError *error = nil;
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *stringToPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/examples/"];
        NSString *stringToFile = [NSString stringWithFormat:@"%@/%@.extension", stringToPath, [self.examples objectAtIndex:indexPath.row]];
        [manager removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:stringToFile] error:&error];
        if(!error){
            NSLog(@"no error");
        }else{
            NSLog(@"%@", error);
        }
        [self.manageObjectContext deleteObject:[self.examples objectAtIndex:indexPath.row]];
        [self.manageObjectContext save:nil];        
        NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"Examples"];
        self.examples = [self.manageObjectContext executeFetchRequest:fetchReq error:&error];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

This is the error when I use [self.examples objectAtIndex:indexPath.row]];

    Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed.
(Cocoa error 4.)" UserInfo=0xb92af90 {NSUnderlyingError=0xb92edb0 "The operation couldn’t be completed.
No such file or directory", NSFilePath=.../Application Support/iPhone Simulator/7.0.3/Applications/8A5B551A-79A1-40E6-B1A4-C948D9F961D0/Documents/example/<Examples: 0xa2b3ef0> (entity:
Examples; id: 0xa2b0310 <x-coredata:/6871E31A-1253-4195-A84D-61C71B002B72/Songs/p21> ;
 data: {
        name = "Example name to delete";
    }).extension, NSUserStringVariant=(
        Remove
    )}
Was it helpful?

Solution 2

I get filename like this:

NSArray *fileName = [self.examples valueForKey:@"name"];
NSString *fileName2 = [fileName objectAtIndex:indexPath.row];

OTHER TIPS

The call to [self.examples objectAtIndex:indexPath.row] appears to be giving back an instance of a Songs object. Your use of this in the stringWithFormat: results in a call to the description method on that object. That certainly is not what you want.

Split the code up a bit:

Songs *songs = [self.examples objectAtIndex:indexPath.row];
NSString *filename = ... // some call on songs to get the filename

Only you know how to get the filename from the songs object.

FYI - the way you build the path is all wrong. Do some searching for the proper way to get a reference to the Documents folder. And do not use stringWithFormat: to build up a path. Use stringByAppendingPathComponent:.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top