Pergunta

I'm trying to programmatically create a folder with Cocoa.

I've written an NSString category and we've got the following function there :

- (void)createAsFolder
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError* err = nil;

    [fileManager createDirectoryAtPath:self withIntermediateDirectories:YES attributes:nil error:&err];

    if (err)
    {
        NSLog(@"ERROR : %@",err);
    }
}

So, in a few words, let's say we have an NSString* path = @"/some/path/is/here";, we can create it simply by :

[path createAsFolder];

The thing is, although it works PERFECTLY for normal folders, it does NOT when the path specified is a bundle (that is : WITH an extension). E.g.

NSString* path = @"/this/is/a/path/to/some/bundle.bun";

[path createAsFolder];

The above does NOT work.

Any ideas on how to fix that?

Foi útil?

Outras dicas

OK, here's the answer (thanks to @thundersteele), if you want to copy a full file tree from on place to another :

NSFileWrapper* w = [[NSFileWrapper alloc] initWithPath:initialPath];

[w writeToFile:destinationPath atomically:YES updateFilenames:YES];

And yep : it has ABSOLUTELY no problem whether the subfolders are packages/bundles or whatever. Not that hard, huh? Just 2 lines... lol

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top