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

时间:2013-11-30 18:41:13

标签: ios

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

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中使用文件夹的方式吗?请帮忙,我疯了!

1 个答案:

答案 0 :(得分:4)

我猜你没有使用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