问题
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?
回答1:
You shold initialize the MutableString before adding strings. NSMutableString* fileList = [[NSMutableString alloc] init ];
来源:https://stackoverflow.com/questions/13500609/nsmutablestring-returns-null