简体   繁体   English

获取使用NSFileManager创建新文件夹的权限

[英]Getting permissions to create a new folder using NSFileManager

I'm having some problems trying to create a folder using NSFileManager in MacOS X. I'm creating the software in Lion if that changes anything. 我在使用MacOS X中的NSFileManager创建文件夹时遇到了一些问题。如果更改了任何内容,我将在Lion中创建该软件。

Anyway. 无论如何。 The program structure is something like this: 程序结构是这样的:

  1. Fill in some fields. 填写一些字段。
  2. Choose a folder using NSOpenPanel. 使用NSOpenPanel选择一个文件夹。
  3. The program generates the name of the new folder from the fields. 该程序从字段中生成新文件夹的名称。
  4. Create a folder with that name in the chosen folder. 在所选文件夹中创建具有该名称的文件夹。

The code that is supposed to create the folder looks like this: 应该创建该文件夹的代码如下所示:

NSString *name = [pnameField stringValue];
NSString *number = [self parseVerknr:[pnumberField stringValue]];
NSString *date = [pdateField stringValue];
NSString *folderName;

//Here I generate the folder name from some rules and stuffsies
//It ends up as something like: 13245_08 - cp. - name

NSOpenPanel *myPanel = [NSOpenPanel openPanel];

    myPanel.canChooseFiles = NO;
    myPanel.canChooseDirectories = YES;
    myPanel.canCreateDirectories = YES;

    [myPanel beginWithCompletionHandler:^(NSInteger result){

        if (result==NSFileHandlingPanelOKButton){

            NSFileManager *myManager = [[NSFileManager alloc] init];

            NSError *merror;

            NSString *directoryString = [NSString stringWithFormat:@"%@%@", [myPanel.URL absoluteString], folderName];

            //Here the directory string looks like file://localhost/Users/username/Documents/folders/12345_09 - cp. - Koop
            directoryString = [directoryString substringFromIndex:6];
            //After it looks like: /localhost/Users/username/Documents/folders/12345_09 - cp. - Koop
            NSLog(@"directory: %@", directoryString);

            [myManager createDirectoryAtPath:directoryString withIntermediateDirectories:YES attributes:nil error:&merror];

            NSLog(@"%@", [merror localizedDescription] );
        }

    }];

If I don't take the substring from index 6 I get a large error stack (tried to take it from 16 too which did the same). 如果我不从索引6中获取子字符串,我会得到一个大的错误堆栈(尝试从16开始也是如此)。 If I use the split string I get the following error: 如果我使用拆分字符串,我会收到以下错误:

You don’t have permission to save the file “localhost” in the folder “PK Bear”.

Since PK Bear is the name of my "hard drive" I'm guessing the program needs to get permission to access the hard drive. 由于PK Bear是我的“硬盘”的名称,我猜这个程序需要获得访问硬盘的权限。 I just have no idea how to do this. 我根本不知道该怎么做。

Any help would be greatly appreciated. 任何帮助将不胜感激。

The problem is the way you're creating the path string. 问题是您创建路径字符串的方式。 In particular, -[NSURL absoluteString] returns a string representation of the URL, not a filepath string. 特别是, -[NSURL absoluteString]返回URL的字符串表示形式,而不是文件路径字符串。 You should use NSURL and NSString's methods for working with paths. 您应该使用NSURL和NSString的方法来处理路径。 As explained in the documentation , -[NSURL path] returns a path string suitable for inputting into NSFileManager methods. 文档中所述, -[NSURL path]返回适合输入NSFileManager方法的路径字符串。

Try this: 尝试这个:

NSString *directoryString = [[myPanel.URL path] stringByAppendingPathComponent:folderName];
[myManager createDirectoryAtPath:directoryString withIntermediateDirectories:YES attributes:nil error:&merror];

You mix up (file-) URLs and (unix) paths. 混合(文件)URL和(unix)路径。

You should create the directory path as follows: 您应该创建目录路径,如下所示:

NSString *directoryString = [[myPanel.URL path] stringByAppendingPathComponent:folderName];
if (! [myManager createDirectoryAtPath:directoryString withIntermediateDirectories:YES attributes:nil error:&merror]) {
    NSLog(@"%@", [merror localizedDescription]);
}

Key is using path instead of absoluteString . 关键是使用path而不是absoluteString

Also: You cannot access the merror object unless the method that creates it returns NO (or whatever the error condition is). 另外:除非创建它的方法返回NO (或任何错误条件),否则无法访问merror对象。

You should never fiddle with single characters in path strings (as you did with substringFromIndex: ). 你永远不应该在路径字符串中使用单个字符(就像你对substringFromIndex:所做的那样)。 That's considered an advanced task in the face of unicode code points vs code fragments and combined characters. 面对unicode代码点与代码片段和组合字符,这被认为是一项高级任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM