NSFileManager无法创建文件 [英] NSFileManager Cant create file

查看:64
本文介绍了NSFileManager无法创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.这段代码不会在Document目录中创建文件,我也不知道为什么.谁能看到我所缺少的.数据"字典具有我所需要的内容,但是涉及writeToFile:它没有发生,因为没有创建文件.我在其他应用程序中具有相同的代码工作,我肯定会丢失一些东西.

I have this code. This code won't create a file in the Document directory and i have no idea why. Can anyone see what I'm missing. "data" dictionary has what i need in it but when it comes to writeToFile: it's not happening because there is no file created. The same code work's in other apps i have, I'm surely missing something.

- (void)addBookmark{

NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]]) 
    {
        [self filePathOfBookmarks];
    }

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];



    if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]]) 
    {
        data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
    }
    else
    {
        // If the file doesn’t exist, create an empty dictionary
        data = [[NSMutableDictionary alloc] init];
    }


    NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
    [firstOne setObject:@"one" forKey:@"name"];
    [firstOne setObject:@"two" forKey:@"call"];
    [firstOne setObject:@"twoandhalf" forKey:@"email"];
    [firstOne setObject:@"four" forKey:@"description"];

    [data setObject:firstOne forKey:@"ID"];



   [data writeToFile: [self filePathOfBookmarks] atomically:YES];

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
    NSLog(@"file content %@",savedStock);
}

- (NSString *) filePathOfBookmarks {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [paths objectAtIndex:0];
    return [docDirectory stringByAppendingPathComponent:@"favourites"];
}


NSLog(@"%@",[self filePathOfBookmarks]);

这将返回:

/Users/spire/Library/Application Support/iPhone Simulator/5.1/Applications/40CAA37F-6477-4101-A142-D4797748ABD9/Documents/favourites

/Users/spire/Library/Application Support/iPhone Simulator/5.1/Applications/40CAA37F-6477-4101-A142-D4797748ABD9/Documents/favourites

推荐答案

请参见下面的代码,它在我这方面运行良好.您刚刚在filePathOfBookmarks方法中提到了目录,而不是确切的文件名.我可以将数据保存在favourites.txt文件中.

See the below code and it's working perfectly from my side. You have just mention directory not exact file name in filePathOfBookmarks method. I can save data on favourites.txt file.

您还可以参考以下链接,了解有关iOS中文件处理的更多信息

You can also refer below links for more info about file handeling in iOS

1)文件保存和加载/使用文档目录存储文件

2) iPhone文件系统

- (void)addBookmark{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]]) 
    {
        [self filePathOfBookmarks];
    }

    NSMutableDictionary *data;// = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];



    if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]]) 
    {
        data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
    }
    else
    {
        // If the file doesn’t exist, create an empty dictionary
        data = [[NSMutableDictionary alloc] init];
    }


    NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
    [firstOne setObject:@"one" forKey:@"name"];
    [firstOne setObject:@"two" forKey:@"call"];
    [firstOne setObject:@"twoandhalf" forKey:@"email"];
    [firstOne setObject:@"four" forKey:@"description"];

    [data setObject:firstOne forKey:@"ID"];



    [data writeToFile: [self filePathOfBookmarks] atomically:YES];

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
    NSLog(@"file content %@",savedStock);
}

- (NSString *) filePathOfBookmarks {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [paths objectAtIndex:0];
    return [docDirectory stringByAppendingPathComponent:@"favourites.txt"];
}

这篇关于NSFileManager无法创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆