0

I just wroted this line:

BOOL directoryResult = [[NSFileManager alloc]
createDirectoryAtURL:[[NSURL alloc]
initFileURLWithPath:[self.documentsPath
stringByAppendingFormat:@"/level%d", levelCount] isDirectory:YES]
withIntermediateDirectories:NO attributes:nil error:nil];
NSLog(@"BOOL: %d", directoryResult);

and I have two questions: how it is possible that this method is working properly? After [NSFileManager alloc] I'm not using init.

Why compiler does not complaining? Is init inside createDirectoryAtURL? Is it good way of programming?

And secondly in URL parameter of createDirectoryAtURL I'm creating NSURL just in place

[[NSURL alloc] initFileURLWithPath:[self.documentsPath stringByAppendingFormat:@"/level%d", levelCount] isDirectory:YES]

same question as above: Is it good way of programming or should I create such object before that line and just put object here?

2 Answers 2

1

[NSFileManager defaultManager] returns singleton instance of the file manager, use it to perform the tasks. It's quite common practice in Cococa. I'm not sure why does your code work properly, I can only guess that this particular method doesn't use any internal variables, so it's valid to call it even without init (although you should never do that).

As for the NSURL construction, the answer depends on compiling options. Do you use ARC? If the answer is 'yes', your code is valid, else it lead to the memory leak. In generat it's better either to create an object and call autorelease (non-ARC apps) explicitly, or use class methods like [NSURL fileURLWithPath:path].

Also, don't treat it as offense, but I believe you're asking this questions in the wrong place. Basic memory management questions should be asked to a good book, one like "Cocoa programming for Mac OS X" by Aaron Hillegass.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! Believe me, yesterday I ordered "Programming in Objective-c, 5th edition" by Stephen G. Kochan from Amazon :)
0

It's not guaranteed that object created without initialization will work properly. So you should init the object. Documentation example:

BOOL isDir=NO;
NSArray *subpaths;
NSString *fontPath = @"/System/Library/Fonts";
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir)
    subpaths = [fileManager subpathsAtPath:fontPath];
[fileManager release];

Also NSFileManager has a shared manager (already created and intialized object)

NSFileManager* fileManager = [NSFileManager defaultManager];

But there is a warning in documentation:

This method always returns the same file manager object. If you plan to use a delegate with the file manager to receive notifications about the completion of file-based operations, you should create a new instance of NSFileManager (using the init method) rather than using the shared object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.