iphone - 带 URL 的 writeToFile - URL 在下一个 session 中不存在

标签 iphone objective-c ios xcode

我正在使用 writeToFile 将音频文件写入本地 iphone 目录。下次启动应用程序时,路径/URL 不再存在,我无法检索我的文件。

非常感谢任何帮助。

保存:

    NSArray *dirPaths;
    NSString *docsDir; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *tempFileName = [[NSString alloc] init];
    tempFileName = [[alertView textFieldAtIndex:0]text];
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.wav", tempFileName]];
    BOOL success = [videoData writeToFile:soundFilePath atomically:YES];

阅读:

if ([[NSFileManager defaultManager] fileExistsAtPath:[urlArray objectAtIndex:indexPath.row]])

其中 urlArray 包含文件的路径,并且此 If 语句为 false。

最佳答案

为什么不将 filePath 保存在 NSMutableArray 和 NSUserDefault 中

 //After writing file in doc dir
 if(success)
 {
   // Store the data
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   if(![defaults objectForKey:@"filePaths"]) //creating mutable array for first time
   {
      NSMutableArray *arrFilePaths = [NSMutableArray alloc]init];
      [defaults setObject:arrFilePaths forKey:@"filePaths"];
   }
   else //saving new soundFilePath in mutable array
   {
      NSMutableArray *arrFilePaths = [defaults objectForKey:@"filePaths"];
      [arrFilePaths addObject:soundFilePath];
      [defaults setObject:arrFilePaths forKey:@"filePaths"];
      [defaults synchronize];
   }

 }

现在你可以这样读:

if([[defaults objectForKey:@"filePaths"] objectAtIndex:indexPath.row])
{
  if ([[NSFileManager defaultManager] fileExistsAtPath:[[defaults objectForKey:@"filePaths"] objectAtIndex:indexPath.row]])
  {
     //here is your file
  }
}

关于iphone - 带 URL 的 writeToFile - URL 在下一个 session 中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152897/

相关文章:

iphone - 高级核心动画/OpenGLES 教程请求

javascript - 如何在 Appcelerator Titanium 项目中组织 JS 文件

ios - 使用 Cocoa 在 Swift 中的性能

ios - UIView 动画和完成 block

ios - UIButton 高度增加大小使用 AutoLayout 与不同的设备

objective-c - 高效的核心数据递归

objective-c - 在 iOS 中从 SQLite 保存和检索图像

ios - 从值中获取数字的最短方法

ios - 如何跟踪内存中地址的值?

iphone 通知结果为 "unrecognized selector sent to instance..."