Robert
Robert

Reputation: 5302

NSMutableString returns null

I'm trying to construct a string from the file names in my app's document folder.

So far I have:

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [directoryPaths objectAtIndex:0];
NSError *error = nil;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

NSLog(@"contents of document folder = %@", directoryContent);

This prints:

    contents of app's document folder = (
    ".DS_Store",
    File1.doc,
    "File2.pdf",
    ....
)

I'd like to construct a string of file names, separated in some way, perhaps by semi-colon and attempted it (initially without any separation) via this code:

NSMutableString *fileList;
for (NSString *folder in directoryContent)
{
    [fileList appendString:[NSString stringWithFormat:@"%@",  folder]];
}
NSLog(@"file list = %@", fileList);

This prints file list = (null), and I can't figure out where I'm going wrong. Any ideas?

Upvotes: 0

Views: 771

Answers (1)

Dave
Dave

Reputation: 1081

You shold initialize the MutableString before adding strings. NSMutableString* fileList = [[NSMutableString alloc] init ];

Upvotes: 1

Related Questions