【问题标题】:writeToFile with URL - URL doesnt exist next session带有 URL 的 writeToFile - 下一个会话 URL 不存在
【发布时间】:2012-08-28 13:06:47
【问题描述】:

我正在使用 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。

【问题讨论】:

  • 网址是怎么做的?
  • 你检查过你的文件已经写入你的目录了吗?

标签: iphone objective-c ios xcode


【解决方案1】:

你为什么不把文件路径保存在 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
  }
}

【讨论】:

    【解决方案2】:

    每次启动应用程序时,您都需要像上面一样在 NSDocument 目录中再次搜索路径。

    您可以在AppDelegate类didFinishLaunching中为filePath创建实例变量来记录路径。

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        docsDir = [dirPaths objectAtIndex:0];
        NSString *tempFileName = [[NSString alloc] initWithString:@"PutYourFileName"];
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.wav", tempFileName]];
    

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多