简体   繁体   中英

saving and retrieving files to an array in documents directory Xcode iOS

I have an app that I want to save a username for each person that I play a game with and then retrieve it into an array that can be used to create headers for a tableview. I am using this code to save the files.

NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *slash2 = [documentsDirectory stringByAppendingString:@"/Names"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:slash2])
        [[NSFileManager defaultManager] createDirectoryAtPath:slash2 withIntermediateDirectories:NO attributes:nil error:&error1];


    //yourName is the userName of the challenger

   NSString *fullPath = [slash2 stringByAppendingPathComponent:yourName];

and when I look at the path

NSLog(@"path :%@",fullPath);

I get

"Names/Mary",
"Names/John",

I would like to then get these names form the documents directory into an array to use is the tableview. I have tried

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *slasha = [documentsDirectory stringByAppendingPathComponent:@"/Names"];


NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:slasha];

for (NSString *element in arrayFromFile)
    NSLog(@"Names: %@", element);

but nothing shows up

What am I doing wrong and how can I do this

user1114881,

Creating folders to show challengers name in tableView header, Seriously ??? I will say its a very wrong approach.

That being said, now let me fix your problem,

NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *slash2 = [documentsDirectory stringByAppendingString:@"/Names"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:slash2])
        [[NSFileManager defaultManager] createDirectoryAtPath:slash2 withIntermediateDirectories:NO attributes:nil error:&error1];


    //yourName is the userName of the challenger

   NSString *fullPath = [slash2 stringByAppendingPathComponent:yourName];
   BOOL Success = [[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil];

Mistake you were doing was, you never created a file for the user, you just created a folder called Names. You need to call createFileAtPath to create file.

In order to read all the files in folder use

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *slasha = [documentsDirectory stringByAppendingPathComponent:@"/Names"];
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:slasha error:nil];

You must be able to see the list of all files inside folder Names now :)

Happy coding.

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