NSMutableString returns null

我只是一个虾纸丫 提交于 2020-01-17 03:34:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!