加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 以编程方式在Xcode中创建文件夹 – 目标C

发布时间:2020-12-14 19:50:01 所属栏目:百科 来源:网络整理
导读:我正在使用以下代码行将我的yoyo.txt文件保存在Documents文件夹中:: NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];NSLog(@"docDir is yoyo :: %@",docDir);NSString *FilePath = [docDir stringByAppendingPathComp
我正在使用以下代码行将我的yoyo.txt文件保存在Documents文件夹中::

NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"docDir is yoyo :: %@",docDir);
NSString *FilePath = [docDir stringByAppendingPathComponent:@"yoyo.txt"];

但是,我希望将文件保存在yoyo的文件夹中,即在Documents文件夹中,即我要创建另一个名为“yoyo”的文件夹,然后将我的yoyo.txt文件保存到其中.我怎样才能做到这一点 ??谢谢.

解决方法

这里是一个示例代码(假设管理器是[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]);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读