objective-c - 使用 Obj-C 重命名现有文件

标签 objective-c

我已经看到这个问题被问过几次,但到目前为止我无法使用任何后期解决方案取得成功。我想做的是在应用程序的本地存储中重命名一个文件(对于 Obj-c 来说也是新的)。我能够检索旧路径并创建新路径,但是我必须写什么才能真正更改文件名?

到目前为止,我得到的是:

- (void) setPDFName:(NSString*)name{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES);
    NSString* initPath = [NSString stringWithFormat:@"%@/%@",[dirPaths objectAtIndex:0], @"newPDF.pdf"];
    NSString *newPath = [[NSString stringWithFormat:@"%@/%@",
                          [initPath stringByDeletingLastPathComponent], name]
                         stringByAppendingPathExtension:[initPath pathExtension]];
}

最佳答案

NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:initPath toPath:newPath error:&error];

关于objective-c - 使用 Obj-C 重命名现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14280989/

相关文章:

objective-c - 将 NSString 减少为小写、ascii 字母字符

iphone - 消息发送到已解除分配的实例问题

ios - 如何编写可空的完成 block ?

ios - 自定义委托(delegate)方法不适用于 iOS

ios - 在模态转换中转换 AVplayer

php - 如何将 PHP mysql_fetch_array() 转换为 NSMutableArray? PHP --> Xcode

objective-c - NSStackView 中单个内容 View 的多个实例

iphone - 连接到 Windows Live Messenger 网络时出现不可能的问题

objective-c - 使用 Grand Central Dispatch 进行文件监控

ios - 升级到 iOS 11.3 破坏了 __weak UIAlertAction