gpt4 book ai didi

iphone - 在 Xcode 中以编程方式创建文件夹 - Objective C

转载 作者:技术小花猫 更新时间:2023-10-29 10:56:53 31 4
gpt4 key购买 nike

我正在使用以下代码行将我的 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com