我已经看到这个问题被问过几次,但到目前为止我无法使用任何后期解决方案取得成功。我想做的是在应用程序的本地存储中重命名一个文件(对于 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/