本文介绍了的NSMutableString返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序的文件夹中的文件名构造一个字符串。

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

到目前为止,我有:

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);

这将打印:

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

我想构建的文件名的字符串,由分号以某种方式分离出来,或许,并且通过此code尝试过(最初没有任何分离):

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?

推荐答案

您shold添加字符串前初始化MutableString。 *的NSMutableString =的fileList [的NSMutableString的alloc]初始化];

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

这篇关于的NSMutableString返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:31