Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iphone上的库目录中创建文件_Iphone_Ios_Nsfilemanager - Fatal编程技术网

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

在iphone上的库目录中创建文件,iphone,ios,nsfilemanager,Iphone,Ios,Nsfilemanager,我正在尝试在我的应用程序库目录的子目录中创建文件。首先,我得到了通向图书馆的路径,如下所示: - (NSURL*) getLibraryDirectory { NSFileManager* manager = [NSFileManager defaultManager]; NSArray* paths = [manager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; if ([path

我正在尝试在我的应用程序库目录的子目录中创建文件。首先,我得到了通向图书馆的路径,如下所示:

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

    if ([paths count] > 0)
    {
        return [paths objectAtIndex:0];
    }
    return nil;
}
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");
    }
}
然后,我使用以下代码创建子文件夹:

- (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;
}
最后一件事是我试图在这个文件夹中创建一些文件,如下所示:

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

    if ([paths count] > 0)
    {
        return [paths objectAtIndex:0];
    }
    return nil;
}
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");
    }
}

但不幸的是,我每次都得到“否”,我不明白为什么。

要从文件URL获取路径,您必须使用
path
而不是
absoluteString

NSString *filePath = [[self getDirectory:DIR_COMMANDS] path];
旁注:对于方法,您应该采用Cocoa的命名风格:
libraryDirectory
,而不是
getLibraryDirectory
,或者更好:
libraryDirectoryURL
。只有通过引用传递返回值时,才会使用
get
前缀

另外:您对
fileExistsAtPath:isDirectory:
的使用不正确。
BOOL
参数通过引用传递:

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

NSLog(@“NO”)之前添加此行
查看有关创建文件路径失败原因的详细错误消息:失败:
NSLog(@“错误为%s”,strerror(errno))谢谢你,尼古拉!最后,它是有效的,而且你所有的评论都非常有用。我会仔细阅读文档。