【问题标题】:How do I read from /Library/Application Support/ folder?如何从 /Library/Application Support/ 文件夹中读取?
【发布时间】:2013-02-15 22:53:22
【问题描述】:

我正在制作一个 OX Cocoa 应用程序,我希望能够在按下按钮时使用该应用程序读取和写入文本文件。这些文本文件应保存在 /Library/Application Support/AppName 但我无法让我的应用程序从那里读取任何内容。它可以写入文件夹,但不能读取其中写入的内容,即使我可以在 finder 中看到该文件。

这是我使用成功写入文件夹的代码。

    NSString *text = editor.string;
    NSString *path = @"/Library/Application Support/";

    NSMutableString *mu = [[NSMutableString stringWithString:path] init];
    [mu insertString:FileName.stringValue atIndex:mu.length];
    [mu insertString:@".txt" atIndex:mu.length];

    path = [mu copy];
    [text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

这是我正在使用(但失败)从文本文件中读取的代码。

    NSArray *path = [[NSBundle mainBundle] pathsForResourcesOfType:@"txt" inDirectory:@"/Library/Application Support/"];
    NSString *output = @"";

    NSMutableString *mu = [[NSMutableString stringWithString:output] init];

    for (int i = 0; i < [path count]; i++) {
        NSString *text = [NSString stringWithContentsOfFile:path[i] encoding:NSUTF8StringEncoding error:NULL];
        [mu insertString:text atIndex:mu.length];
        [mu insertString:@"\n" atIndex:mu.length];
    }

    [textView setString:mu];

任何关于我可以纠正的提示都会非常有帮助,我有点卡在这里。

编辑:使用您的输入,我已将代码更新为:

    NSString *fileLocation = @"~/Library/Application Support/";
    NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:nil];
    NSString *output = @"";
    NSMutableString *mu = [[NSMutableString stringWithString:output] init];

    for (int i = 0; i < [text count]; i++) {
        [mu insertString:text[i] atIndex:mu.length];
        [mu insertString:@"\n" atIndex:mu.length];
    }
    [textView setString:mu];

但是文件中的文本仍然没有出现。

【问题讨论】:

    标签: xcode macos cocoa readfile


    【解决方案1】:

    当您对应用进行沙箱处理时,大多数硬编码路径都会失败。即使您侥幸逃脱,或者您不打算将此应用程序沙盒化,这也是一个值得改掉的坏习惯。

    此外,您确定要/Library 而不是~/Library?前者通常不是用户可写的。后者位于用户的主目录(或沙盒时的容器)中。

    要获取应用程序支持目录、缓存目录或任何其他您可能想要在其中创建内容并稍后从中检索它们的目录,ask a file manager for it

    【讨论】:

      【解决方案2】:

      /Library/Application Support 不在您的捆绑包中。您使用[[NSBundle mainBundle] pathsForResourcesOfType:…] 获得的路径仅对访问应用程序本身内部的文件有用(您在构建应用程序时包含的图像、声音等)。

      您想使用[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:error] 获取应用程序外部目录中的文件列表。

      Matt Gallagher 提供了一个很好的示例,说明了在Cocoa With Love 定位应用程序支持目录的路径的容错方法。我建议使用它而不是硬编码 /Library/Application Support 路径。

      NSError *error = nil;
      NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:&error];
      if (!text) {
          NSLog( @"Error reading contents of application support folder at %@.\n%@", applicationSupportFolder, [error userInfo] );
      }
      

      【讨论】:

      • 在您的原始代码中编写您指向/Library/Application Support 的文件。您是否更改了它以匹配您在此处使用的 ~/Library/Application Support?
      • ~ 有影响吗?
      • 是的。 ~ 扩展为 /Users/*username*/Library/Application Support,没有 ~ 它进入全局 /Library/Application Support 文件夹。
      • 很高兴知道,但它不适用于其中任何一个,我想使用哪一个?
      • 好的,所以我收到错误“有一个文件,需要一个文件夹”或类似的东西,所以我从路径末尾删除了 test file.txt,它输出了文件夹中的文件名.这是一个进步,但我希望输出单个文件的内容。
      【解决方案3】:

      您正在尝试使用 NSBundle 从应用程序的主包中获取路径。但是该文件不在捆绑包中,您应该手动指定路径。您可以对路径进行硬编码,将以前编写的路径存储在某处,或者使用 NSFileManager 获取目录内容并对其进行分析。例如,-[NSFileManager contentsOfDirectoryAtPath:error:]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-24
        • 2016-02-10
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 2015-03-23
        相关资源
        最近更新 更多