PDA

View Full Version : NSFileManger Question




DavidBlack
Jul 31, 2013, 07:41 PM
Hi, I have two folders on my desktop named "origin" and "destination". I want to move the origin folder to the destination folder.

So I used this code NSFileManager* fileManager = [NSFileManager defaultManager];
[fileManager moveItemAtURL:sourceURL toURL:sourceURL2 error:nil];


But the won't move can anyone help?



larswik
Jul 31, 2013, 08:17 PM
Have you tried using an NSError. That method provides an error if there is an error to find out what is going on. You assigned it to nil.


NSError *error;
[theMethod error:&error];

if (error){
//NSLog my error

DavidBlack
Jul 31, 2013, 08:18 PM
Have you tried using an NSError. That method provides an error if there is an error to find out what is going on. You assigned it to nil.


NSError *error;
[theMethod error:&error];

if (error){
//NSLog my error


Thanks I will include and report back my results.

gnasher729
Aug 1, 2013, 06:42 AM
Have you tried using an NSError. That method provides an error if there is an error to find out what is going on. You assigned it to nil.


NSError *error;
[theMethod error:&error];

if (error){
//NSLog my error


Better: NSError* error = nil;

Methods using NSError** are supposed to not change the error variable on success, so testing error after a successful call will likely lead to a crash.

On the other hand, it would be a good exercise to turn warnings on in Xcode until the original code without the initialisation doesn't compile anymore.

chown33
Aug 1, 2013, 12:32 PM
Have you tried using an NSError. That method provides an error if there is an error to find out what is going on. You assigned it to nil.


NSError *error;
[theMethod error:&error];

if (error){
//NSLog my error


Don't do this.

The NSFileManager method returns a boolean. Check that first for success or failure. Only on failure should a returned NSError* be referenced. On success, there is no guarantee that the **NSError will be nil, even if it was set to nil before the call.

Reference doc Error Handling Programming Guide (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html), section "Using and Creating Error Objects"

Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.

larswik
Aug 1, 2013, 02:49 PM
OK, I can see how you need to assign the results to a BOOL and check the BOOL for true or false. I have been doing it this way without problems but I can see the reasons why to set it up.

I always thought that with an if statements results evaluated to true it would execute the if statement, if not just bypass it. So even if the the method with error was successful and did not return an error, meaning that the error was NULL, or did not exist, the if statement would default to false because it was not true.