目标C - 如何将文件从一个位置移动到另一个位置?

时间:2015-02-09 23:25:09

标签: objective-c xcode macos

目标C的初学者,我有这个代码:

//Path of management log file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *urlPath= [documentsDirectory stringByAppendingPathComponent:@"Logs/log.txt"];

NSURL *source = [NSURL fileURLWithPath:urlPath];

//Destination
NSURL * destination = [self.pathControl URL];

//Move file from source to that destination directory.
[[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];

问题在于它什么都不做。它可以编译,但没有任何反应。我使用copyItemAtURL方法错了吗?

1 个答案:

答案 0 :(得分:1)

使用错误指示器来理解错误:

NSError *error;
BOOL status = [[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:&error];
if (status == NO ) {
    NSLog(@"errorL %@", error);
}