gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:27:19 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com