在 iPhone 中创建一个目录

发布于 2024-08-02 20:29:03 字数 389 浏览 4 评论 0原文

这有什么问题吗?

#define AUDIO_NOTES_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myApp/Pictures"]

NSFileManager *NSFm= [NSFileManager defaultManager]; 
BOOL isDir=YES;

if(![NSFm fileExistsAtPath:FILEPATH isDirectory:&isDir])
    if(![NSFm createDirectoryAtPath:FILEPATH attributes:nil])
        NSLog(@"Error: Create folder failed");

What's wrong with that?

#define AUDIO_NOTES_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myApp/Pictures"]

NSFileManager *NSFm= [NSFileManager defaultManager]; 
BOOL isDir=YES;

if(![NSFm fileExistsAtPath:FILEPATH isDirectory:&isDir])
    if(![NSFm createDirectoryAtPath:FILEPATH attributes:nil])
        NSLog(@"Error: Create folder failed");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2024-08-09 20:29:03

createDirectoryAtPath:attributes: 已弃用,您应该使用:

    NSString *dirToCreate = [NSString stringWithFormat:@"%@/newDirectory",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
    NSError *error = nil;
    BOOL isDir;
    if(![fm fileExistsAtPath:dirToCreate isDirectory:&isDir])
        if(![fm createDirectoryAtPath:dirToCreate withIntermediateDirectories:YES attributes:nil error:&error])
            NSLog(@"Error: Create folder failed");

createDirectoryAtPath:attributes: is deprecated, instead you should use:

    NSString *dirToCreate = [NSString stringWithFormat:@"%@/newDirectory",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
    NSError *error = nil;
    BOOL isDir;
    if(![fm fileExistsAtPath:dirToCreate isDirectory:&isDir])
        if(![fm createDirectoryAtPath:dirToCreate withIntermediateDirectories:YES attributes:nil error:&error])
            NSLog(@"Error: Create folder failed");
夜清冷一曲。 2024-08-09 20:29:03

FILEPATH 标记未定义 - 您在文件开头#define AUDIO_NOTES_FOLDER,然后在代码中使用FILEPATH

另请注意, NSHomeDirectory() 不一定是查找文档目录的推荐方法 - 相反,您可能需要:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

The FILEPATH token is undefined - you #define AUDIO_NOTES_FOLDER at the beginning of your file, then use FILEPATH instead in your code.

Also note that NSHomeDirectory() isn't necessarily the recommended way of finding the Documents directory anymore - instead you probably want:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文