【问题标题】:write content of nshomedirectory in array将nshomedirectory的内容写入数组
【发布时间】:2011-05-06 19:16:25
【问题描述】:

我是 iPhone 编程新手。

我想把 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 的方法?!?

请帮帮我

【问题讨论】:

  • 请使用 cmets 在个别答案下请求澄清。答案本身应该就是解决您问题的解决方案。

标签: uitableview nsstring nsmutablearray


【解决方案1】:

你试过用返回的数组构造一个 NSMutableArray 吗?

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

或者在返回的数组上使用mutableCopy

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

【讨论】:

  • 您好,感谢您提供的信息和示例代码。主目录不是我的应用程序的目录吗?!?我应该在哪个目录下载文件?!?
  • @DennisW83:the docs 中没有提到,但您似乎是对的。我会将文件下载到NSTemporaryDirectory(),或NSSearchPathForDirectoriesInDomains() 返回的目录(以NSDocumentDirectoryNSCachesDirectoryNSApplicationSupportDirectory 作为目录,NSUserDomainMask 作为域掩码),具体取决于我要做什么处理它。
猜你喜欢
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2012-01-28
  • 2015-11-12
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多