gpt4 book ai didi

macos - NSFileManager 说文件不可写 Mac

转载 作者:行者123 更新时间:2023-12-03 17:04:25 25 4
gpt4 key购买 nike

我一直在尝试使用 AVFoundation 来记录屏幕输出。由于未知原因,在我升级到最新版本的 Mac(Mountain Lion)后,它停止工作。我一直在努力让它发挥作用,但到目前为止还没有成果。我知道如果输出文件已存在,AVFoundation 方法 startRecordingToOutputFileURL 将不起作用。因此,我尝试使用 NSFileManager 来查看我的目标文件是否存在以及是否可写。我的文件管理器始终返回与目标文件不存在且不可写相对​​应的值。我尝试设置文件权限无济于事,任何人都可以阐明我可能的错误:

dest = [[NSURL alloc] initFileURLWithPath:@"~/Desktop/myMovie.mov"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSNumber numberWithInt:777] forKey:NSFilePosixPermissions]; //I tried 511 too, no avail
[fileManager setAttributes:attributes ofItemAtPath:[dest path] error:nil];
if (![fileManager fileExistsAtPath:[dest path]]) {
if ([fileManager isWritableFileAtPath:[dest path]]) {
/* Starts recording to a given URL. */
[captureMovieFileOutput startRecordingToOutputFileURL:dest recordingDelegate:self];
}
else{
NSLog(@"File doesnot exist but is not writable"); //This is the message I get as result
}

}
else
{
NSLog(@"File Exists...");
}

最佳答案

未展开的波形符在 Cocoa 中不是有效路径。您必须在传递到 NSURL-initFileURLWithPath: 的字符串上使用 -stringByExpandingTildeInPath 或更好的 -stringByStandardizingPath

因此,NSFileManager 将为 isWritableFileAtPath 返回 NO,因为它是无效路径(因此不可写)。这会导致你的 NSLog() 被解雇。

根据评论更新:

您可能仍然会发现 NSURL 在创建时返回 nil(因此调用 -path 将返回 nil),因为路径仍然无效。另外值得注意的是,文档对于 -isWritableFileAtPath 说:“尝试一个操作(例如加载文件或创建目录)、检查错误并优雅地处理这些错误比尝试提前弄清楚要好得多操作是否成功需要一定的时间。”

采纳 Peter Hosey 的建议,如果在尝试写入文件时调用失败,请使用 NSError,并且不要尝试提前弄清楚它。

关于macos - NSFileManager 说文件不可写 Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072398/

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