简体   繁体   English

在iphone上的Library目录中创建文件

[英]Creating file in Library directory on iphone

I'm trying to create files in subdirectory inside my app Library dir. 我正在尝试在我的应用程序库目录中的子目录中创建文件。 First at all I'm getting path to Library like this: 首先,我正在通往图书馆这样的道路:

- (NSURL*) getLibraryDirectory
{
    NSFileManager* manager = [NSFileManager defaultManager];
    NSArray* paths = [manager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];

    if ([paths count] > 0)
    {
        return [paths objectAtIndex:0];
    }
    return nil;
}

Then I create subfolder using this code: 然后我使用以下代码创建子文件夹:

- (NSURL*) getDirectory:(NSString*)subdirName
{
    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSURL* libraryDirectory = [self getLibraryDirectory];

    if (libraryDirectory)
    {
        NSURL* subdir = [libraryDirectory URLByAppendingPathComponent:subdirName isDirectory:YES];
        if (![sharedFM fileExistsAtPath:[subdir absoluteString] isDirectory:YES])
        {
            NSError* error;
            if ([sharedFM createDirectoryAtURL:subdir withIntermediateDirectories:YES attributes:nil error:&error])
            {
                return subdir;
            } 
            else 
            {
                NSLog(@"Error occured while trying to create subdirectory \"%@\". Code - %d, desc - %@", subdirName, [error code], [error localizedDescription]);
            }
        }
    }
    return nil;
}

and last thing I'm trying to create some file in this folder like this: 最后我想在这个文件夹中创建一些文件,如下所示:

NSString* filePath = [[self getDirectory:DIR_COMMANDS] absoluteString];
if (filePath)
{
    filePath = [filePath stringByAppendingPathComponent:@"test_file.tst"];       

    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager createFileAtPath:filePath contents:[[NSData alloc] initWithBytes:[[@"string" dataUsingEncoding:NSUTF16LittleEndianStringEncoding] bytes] length:12] attributes:nil])
    {
        NSLog(@"YES");
    }
    else
    {
        NSLog(@"NO");
    }
}

But unfortunately I'm getting "NO" every time and can't understand why. 但不幸的是,我每次都“不”,不明白为什么。

To get a path from a file URL you have to use path instead of absoluteString . 要从文件URL获取路径,您必须使用path而不是absoluteString

NSString *filePath = [[self getDirectory:DIR_COMMANDS] path];

Side note: You should adopt Cocoa's naming style for methods: libraryDirectory instead of getLibraryDirectory , or even better: libraryDirectoryURL . 旁注:你应该为方法采用Cocoa的命名方式: libraryDirectory而不是getLibraryDirectory ,甚至更好: libraryDirectoryURL The get prefix is only used if return values are passed by reference. 只有在通过引用传递返回值时才使用get前缀。

Also: Your usage of fileExistsAtPath:isDirectory: is incorrect. 另外:您对fileExistsAtPath:isDirectory:的使用不正确。 The BOOL parameter is passed by reference: BOOL参数通过引用传递:

BOOL isDir;
if ([sharedFM fileExistsAtPath:[subdir path] isDirectory:&isDir]) {
    if (! isDir) {
        NSLog(@"There's a plain file at my path");
        return nil;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM