MST

星途 面试题库

面试题:Objective-C中NSFileManager实现文件复制操作

在Objective-C中,使用NSFileManager实现将一个文件从源路径复制到目标路径的功能,需考虑错误处理。请写出关键代码片段。
37.8万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *sourcePath = @"/path/to/source/file";
        NSString *destinationPath = @"/path/to/destination/file";
        
        NSError *error;
        BOOL success = [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
        if (success) {
            NSLog(@"文件复制成功");
        } else {
            NSLog(@"文件复制失败: %@", error);
        }
    }
    return 0;
}