iosxcodefor-loopnsarraynsmutablearray

Get Contents of Files In Array on iOS


On my iOS app, I need to look through one of the document directories which has stored multiple created PLISTs. Each PLIST simply contains an Array of names. What I need to do is for each PLIST, get the name of the file, and the array of names within the file. I'm stuck at the best way to go about doing this. When I run various logs, I get the correct path, but the Array always returns Null.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     
     NSString *devotionals = [documentsDirectory stringByAppendingPathComponent:@"devotionals"];
     self.files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:devotionals error:nil];
     self.Array = [NSMutableArray arrayWithCapacity:[self.files count]];

     for (id myArrayElement in self.files) {
         NSLog(@"names%@", myArrayElement);
         
         self.theRealDeal = [devotionals stringByAppendingPathComponent:myArrayElement];
         [self.Array addObject:self.theRealDeal];
         NSLog(@"REALDEAL %@", self.theRealDeal);
         NSLog(@"ARRAY %@", self.Array);
         
         

        // NSLog(@"Loop %@", self.Array);
     }

The PLIST looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>A Common Love</string>
    <string>A Shield About Me</string>
    <string>A Wonderful Savior</string>
    <string>Above All</string>
    <string>Above All Else</string>
    <string>Ah, Lord God</string>
    <string>Alas and Did</string>
    <string>All Because of God's Amazing Grace</string>
</array>
</plist>

UPDATE:

I changed the code a little bit in my OP. According to the logs, self.theRealDeal will give me paths to every single PLIST in the directory. I try to add those paths to an NSMutableArray but the NSLog just shows (


Solution

  • Here is how I got it to work.

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject: @"LoggedIn" forKey:@"isLogged"];
            [defaults synchronize];
            
            
            
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *devotionals = [documentsDirectory stringByAppendingPathComponent:@"devotionals"];
    
            self.files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:devotionals error:nil];
           // NSLog(@"FILES %lu", (unsigned long)[self.files count]);
            self.Array = [[NSMutableArray alloc] initWithCapacity:[self.files count]];
            //self.Array = [NSMutableArray arrayWithCapacity:[self.files count]];
    
            for (id myArrayElement in self.files) {
                
                self.theRealDeal = [devotionals stringByAppendingPathComponent:myArrayElement];
                [self.Array addObject:self.theRealDeal];
            
            }
            for (id finishHim in self.Array) {
                self.Array = [[NSMutableArray alloc] initWithContentsOfFile:finishHim];
                NSString *theFileName = [[finishHim lastPathComponent] stringByDeletingPathExtension];
               
                
                
            }