简体   繁体   中英

Saving and retrieving files from a folder in documents directory IOS

I am trying to save and retrieve a file from a folder in the documents directory. I retrieve it in this way:

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

and save it like this:

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

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

For some reason if I do:

NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/FOLDER", docsPath] error:nil];

and

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

It doesn't open or show the file, but when I log the two it shows the same directory. Is this how you use folders in IOS? Please help, i'm going crazyyyy!!

I guess you use the NSFileManager for nothing. it's confusing how you store and use your data. Here's a class you could use to load, store and delete data into the 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

I don't really understand what you want to do but a good way to use this class could be:

NSArray *contents = [DKStoreManager loadBusinessCardContentForKey:aBusinessCard.name];
[DKStoreManager storeBusinessCardContent:aContent forKey:aBusinessCard.name];
[DKStoreManager removeBusinessCardForKey:aBusinessCard.name];

By the way you can store any data/object you want with this class: NSArray, NSDictionnary,... and even your own class the only thing you need to do is to implement the NSCoding protocol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM