gpt4 book ai didi

ios - 从文档目录IOS中的文件夹保存和检索文件

转载 作者:行者123 更新时间:2023-12-01 18:17:33 24 4
gpt4 key购买 nike

我正在尝试从documents目录中的文件夹保存和检索文件。我以这种方式检索它:

NSFileManager *fManager = [NSFileManager defaultManager];
NSString *item;
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@", docsPath] error:nil];

并保存如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString* BusinessCardPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"BusinessCard%lld.card", arc4random() % 100000000000000]];

由于某些原因,如果我这样做:
NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/FOLDER", docsPath] error:nil];


NSString* BusinessCardPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"FOLDER/BusinessCard%lld.card", FolderNumber, arc4random() % 100000000000000]];

它不会打开或显示文件,但是当我登录两个文件时,它会显示相同的目录。这是您在IOS中使用文件夹的方式吗?请帮助,我要疯了!

最佳答案

我想您什么都不用使用NSFileManager。这使您难以存储和使用数据。
这是您可以用来将数据加载,存储和删除到NSDocumentDirectory中的类:

DKStoreManager.h

@interface DKStoreManager : NSObject

+ (NSArray *)loadBusinessCardContentForKey:(NSString *)key;
+ (void)storeBusinessCardContent:(NSArray *)content forKey:(NSString *)key;
+ (void)removeBusinessCardForKey:(NSString *)key;

@end

DKStoreManager.m
@interface DKStoreManager () {
NSString * _rootPath;
}
@end

@implementation DKStoreManager

- (id)init {
self = [super init];
if (self) {
// get the root path of the Document Directory
// NSCacheDirectory is also good to use
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_rootPath = [paths objectAtIndex:0];
}
return self;
}

+ (DKStoreManager *)sharedInstance {
static DKStoreManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[DKStoreManager alloc] init];
});
return sharedInstance;
}

#pragma mark - storing management methods

// store a data into a file in a specific sub directory
- (id)storeObject:(id)object inFile:(NSString *)filename inDirectory:(NSString *)directory {
NSString *fullPath = [_rootPath stringByAppendingPathComponent:directory];
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath])
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];

fullPath = [fullPath stringByAppendingPathComponent:filename];
BOOL result = [NSKeyedArchiver archiveRootObject:object toFile:fullPath];
if (result)
NSLog(@"Successfully saved %@/%@", directory, filename);
else
NSLog(@"ERROR: can't save %@/%@", directory, filename);

return (result ? object : nil);
}

// remove a file in a specific sub directory
- (void)removeFile:(NSString *)filename inDirectory:(NSString *)directory {
NSString *fullPath = [_rootPath stringByAppendingPathComponent:directory];
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath])
return ;

fullPath = [fullPath stringByAppendingPathComponent:filename];
NSError *error = [NSError new];
if ([[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error])
NSLog(@"Successfully removed %@/%@", directory, filename);
else
NSLog(@"ERROR: can't remove %@/%@ : %@", directory, filename, [error localizedDescription]);
}

// get the data stored into a file
- (id)loadObjectInFile:(NSString *)filename inDirectory:(NSString *)directory {
NSString *fullPath = [_rootPath stringByAppendingPathComponent:directory];
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath])
return nil;

fullPath = [fullPath stringByAppendingPathComponent:filename];
return [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath];
}

#pragma mark - business cards methods

+ (NSArray *)loadBusinessCardContentForKey:(NSString *)key {
DKStoreManager *storeManager = [DKStoreManager sharedInstance];
return [storeManager loadObjectInFile:key inDirectory:@"business_cards"];
}

+ (void)storeBusinessCardContent:(NSArray *)content forKey:(NSString *)key {
DKStoreManager *storeManager = [DKStoreManager sharedInstance];
[storeManager storeObject:content inFile:key inDirectory:@"business_cards"];
}

+ (void)removeBusinessCardForKey:(NSString *)key {
DKStoreManager *storeManager = [DKStoreManager sharedInstance];
[storeManager removeFile:key inDirectory:@"business_cards"];
}

@end

我不太了解您想做什么,但是使用此类的一种好方法可能是:
NSArray *contents = [DKStoreManager loadBusinessCardContentForKey:aBusinessCard.name];
[DKStoreManager storeBusinessCardContent:aContent forKey:aBusinessCard.name];
[DKStoreManager removeBusinessCardForKey:aBusinessCard.name];

顺便说一下,您可以使用此类存储所需的任何数据/对象:NSArray,NSDictionnary等,甚至您自己的类,您唯一需要做的就是实现 NSCoding protocol

关于ios - 从文档目录IOS中的文件夹保存和检索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20304149/

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