Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 如何读取/Library/Application Support/folder?_Xcode_Macos_Cocoa_Readfile - Fatal编程技术网

Xcode 如何读取/Library/Application Support/folder?

Xcode 如何读取/Library/Application Support/folder?,xcode,macos,cocoa,readfile,Xcode,Macos,Cocoa,Readfile,我正在制作一个OX Cocoa应用程序,我希望能够使用应用程序上的按钮来读取和写入文本文件。这些文本文件应该保存在/Library/Application Support/AppName中,但我无法让我的应用程序从那里读取任何内容。它可以写入文件夹,但不能读取它在那里写入的内容,即使我可以在finder中看到文件 下面是我正在使用的代码,成功写入文件夹 NSString *text = editor.string; NSString *path = @"/Library/Appl

我正在制作一个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];
NSArray*path=[[NSBundle mainBundle]路径ForResourceSoftType:@“txt”目录:@“/库/应用程序支持/”;
NSString*输出=@“”;
NSMutableString*mu=[[nsmutablestringwithstring:output]init];
对于(int i=0;i<[路径计数];i++){
NSString*text=[NSString stringWithContentsOfFile:path[i]编码:NSUTF8StringEncoding错误:NULL];
[mu insertString:text atIndex:mu.length];
[mu insertString:@“\n”atIndex:mu.length];
}
[textView设置字符串: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];
NSString*fileLocation=@“~/Library/Application Support/”;
NSArray*text=[[NSFileManager defaultManager]目录目录路径:文件位置错误:nil];
NSString*输出=@“”;
NSMutableString*mu=[[nsmutablestringwithstring:output]init];
对于(int i=0;i<[文本计数];i++){
[mu insertString:text[i]atIndex:mu.length];
[mu insertString:@“\n”atIndex:mu.length];
}
[textView设置字符串:mu];

但是,文件中的文本仍然没有显示

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

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

马特·加拉赫(Matt Gallagher)提供了一个容错方法的很好例子,该方法可以定位应用程序支持目录的路径。我建议使用它而不是硬编码/Library/applicationsupport路径

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

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

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

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


要获取应用程序支持目录、缓存目录或您可能希望在其中创建内容并稍后从中检索内容的任何其他目录,请在原始代码中。

编写您指向的文件/Library/Application Support。您是否将其更改为与您在此处使用的~/Library/Application支持相匹配?使用~~是否会有所不同?是的。~扩展为/Users/*username*/Library/Application Support,但没有~s,它将进入全局/Library/Application Support文件夹。很高兴知道,但这两个文件夹都不起作用,我想使用哪一个?好的,因此出现了错误“获取文件,需要文件夹”或者类似的东西,所以我从路径的末尾删除了testfile.txt,它将文件名输出到文件夹中。这是一个进步,但我希望输出单个文件的内容。