2011-05-06 61 views
0

我是iPhone编程中的新手。我想获取NSHomeDirectory的内容到一个数组。在数组中写入nshomedirectory的内容

我发现这个代码

NSError *error; 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 

    // Point to Document directory 
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

    // Write out the contents of home directory to console 
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]]; 

可能有人给我一个方法来将文件放在一个NSMutableArray?!?

请帮我一下

+0

请使用注释请求个别答案下的说明。答案本身应该就是这样,解决方案可以解决你的问题。 – 2011-05-07 10:25:29

回答

1

您是否试过用返回的数组构造一个NSMutableArray?

NSMutableArray *files = [NSMutableArray arrayWithArray:[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]]; 

或在返回的数组上使用mutableCopy

NSMutableArray *files = [[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error] mutableCopy]; 
+0

嗨,谢谢你的信息和示例代码。不是主目录我的应用程序的目录?!?我应该在哪个目录下载文件?!? – DennisW83 2011-05-07 07:51:15

+0

@ DennisW83:[文档]中未提及(http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSHomeDirectory),但看起来你是对的。我将文件下载到'NSTemporaryDirectory()',或者由'NSSearchPathForDirectoriesInDomains()'(NSDocumentDirectory','NSCachesDirectory'或'NSApplicationSupportDirectory'作为目录,'NSUserDomainMask'作为domainMask)返回的目录,具体取决于我将如何处理它。 – Anomie 2011-05-07 15:20:46

+0

非常感谢您的信息! – DennisW83 2011-05-09 06:26:26