我正在使用以下代码行将我的 yoyo.txt 文件保存在 Documents 文件夹中::
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"docDir is yoyo :: %@", docDir);
NSString *FilePath = [docDir stringByAppendingPathComponent:@"yoyo.txt"];
但是,我希望将我的文件保存在 yoyo 的文件夹中,即在 Documents 文件夹中,即我想创建另一个名为“yoyo”的文件夹,然后将我的 yoyo.txt 文件保存到其中。我怎样才能做到这一点 ??谢谢。
最佳答案
这是一个示例代码(假设 manager
是 [NSFileManager defaultManager]
):
BOOL isDirectory;
NSString *yoyoDir = [docDir stringByAppendingPathComponent:@"yoyo"];
if (![manager fileExistsAtPath:yoyoDir isDirectory:&isDirectory] || !isDirectory) {
NSError *error = nil;
NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey];
[manager createDirectoryAtPath:yoyoDir
withIntermediateDirectories:YES
attributes:attr
error:&error];
if (error)
NSLog(@"Error creating directory path: %@", [error localizedDescription]);
}
关于iphone - 在 Xcode 中以编程方式创建文件夹 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187716/