gpt4 book ai didi

iOS:无法获取缓存目录的内容

转载 作者:技术小花猫 更新时间:2023-10-29 10:47:57 27 4
gpt4 key购买 nike

尝试获取缓存目录的内容:

NSError *error;
NSString *path = [[self.class cachesDirectoryURL] absoluteString];
NSArray *directoryItems = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

path 是正确的,我可以在 Finder 中看到它存在并且包含我的文件。
directoryItemsnil 并且 error

Error Domain=NSCocoaErrorDomain Code=260 "The folder “Caches” doesn’t exist." UserInfo={NSUnderlyingError=0x7982d2d0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSFilePath=file:///Users/seelts/Library/Developer/CoreSimulator/Devices/C1411C3D-91FB-4D91-83A4-D0747071AE36/data/Containers/Data/Application/800DE429-F10C-438E-BB04-2948C0B07E7C/Library/Caches/, NSUserStringVariant=(
Folder
)}

我怎么了?

最佳答案

您使用的路径错误。要为应用程序获取正确的缓存目录,请使用:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];

cacheDirectory 中,您将收到这样的字符串路径(没有 file 方案):

/Users/username/Library/Developer/CoreSimulator/Devices/D55805E2-50FF-4D8D-8D04-490001AD6071/data/Containers/Data/Application/DE437461-E0F8-4C11-861E-2D1C56CDF6F1/Library/Caches


完整代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];

NSError *error;
NSArray *directoryItems = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cacheDirectory error:&error];
NSLog(@"%@", directoryItems);

关于iOS:无法获取缓存目录的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35439491/

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