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

10-12 03:36