Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
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
Code:
 NSFileManager* fileManager = [NSFileManager defaultManager];
    [fileManager moveItemAtURL:sourceURL toURL:sourceURL2 error:nil];

But the won't move can anyone help?
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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.

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

if (error){
  //NSLog my error
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.

Code:
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

Moderator
Staff member
Aug 9, 2009
10,758
8,447
A sea of green
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.

Code:
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, 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

macrumors 68000
Sep 8, 2006
1,552
11
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.