سؤال

كنت أعاني من هذا واحد لبعض الوقت ، لذلك نرحب بأي تلميح أو اقتراح.

أحاول حذف ملف من دليل تحت "المستندات". المشكلة هي أن الملف لا يتم حذفه على Dow الجهاز الموجود على جهاز المحاكاة. فقط للإضافة إلى الغموض ، قبل الاتصال إلى RemoveItemAtpath ، أتحقق مما إذا كان الملف موجودًا مع FileExiStatPath ، وحتى عرض قائمة الملفات أسفل هذا المجلد.

إرفاق رمز الحذف:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete file
NSString *directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
NSString *path = [directory stringByAppendingPathComponent:[mListOfItems objectAtIndex:indexPath.row]];
NSError *error = nil;

// Check if FileName doesn't exist.
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"File Deletion Failed" message:[NSString stringWithFormat:@"File %@ not found.", [mListOfItems objectAtIndex:indexPath.row]]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alertView show];
 [alertView release];   
}
else {

  FAILED HERE ---V  
        if (![fileManager removeItemAtPath:path error:&error]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error While Deleting File" message:[NSString stringWithFormat:@"%@", error]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];   

 }
 else {
// delete row from local data
[self.tableView beginUpdates];
[mListOfItems removeObjectAtIndex:indexPath.row];

// Delete the row from view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
 }// Delete - success
}// File Exist
}// Edit - Delete   
}

الرمز الذي يعرض قائمة الملفات في هذا المجلد ...

   - (void) handleFilesRowTapped
 {

 // Get list of files to be displayed
 NSFileManager *manager = [NSFileManager defaultManager];
   NSError *error = nil;
 NSString * directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
 NSArray *fileList = [manager contentsOfDirectoryAtPath:directory error:&error];

 // Allocate file list view controller
 FileListViewController *fileListViewController = [[FileListViewController alloc] initWithNibName:@"FileListViewController" bundle:nil];

 //// Make sure that only *.TXT files are displayed

 // Set the list to be displayed
 fileListViewController.mListOfItems = [[NSMutableArray alloc] init];
 for (NSString *fileName in fileList) {
  if (([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)]     compare:@".txt"] == NSOrderedSame) || 
 ([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)] compare:@".TXT"] == NSOrderedSame)){
   [fileListViewController.mListOfItems addObject:fileName];
  }
 }

 // Configure viwe controller
 [fileListViewController setTitle:@"Files"];

 // Push for display
 [self.navigationController pushViewController:fileListViewController animated:YES];
 [fileListViewController release];

    }

شكرا جزيلا.

هل كانت مفيدة؟

المحلول

هذه:

[[NSBundle mainBundle] resourcePath]

... يعيد طريق مثل:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/i4TestBed.app

... وهو الطريق إلى حزمة التطبيق نفسها التي تتم قراءة فقط. بمجرد الامتثال للتطبيق ، لا يمكن تغيير محتويات حزمة في أي حال.

للعثور على ملف في مجلد المستندات استخدام:

NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath=[docPaths objectAtIndex:0];

... الذي يعيد مسار مثل:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/Documents

إلحاق اسم الملف وأنت على ما يرام.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top