NSFileManager无法创建文件

时间:2012-04-05 09:07:11

标签: iphone objective-c ios nsfilemanager

我有这个代码。此代码不会在Document目录中创建文件,我不知道为什么。任何人都可以看到我所缺少的东西。 “data”字典有我需要的东西但是当涉及到writeToFile时:它没有发生,因为没有创建文件。我在其他应用程序中使用相同的代码工作,我肯定错过了一些东西。

- (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 / favorites

2 个答案:

答案 0 :(得分:0)

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

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

1)file saving-and loading / using the document directory to store files

2)iPhone File System

- (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"];
}

答案 1 :(得分:0)

你的一些代码对我来说没有意义。

这没有做任何事情:

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];
}

这是有道理的。但是当你省略前两个代码块时,请确保声明数据对象:

NSMutableData *data;

然后将firstOne对象设置为数据字典。 这似乎很好(尽管使用此代码,之前从现有书签文件加载的数据根本没有使用)。

编写数据字典似乎也很好:

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

当然,你的方法filePathOfBookmarks返回一个有效的文件名。

然而,该方法返回目录,而不是文件。添加如下后缀:

return [docDirectory stringByAppendingPathComponent:@"favorites.plist"];

使用

检查文件路径的有效性
NSLog(@"%@",[self filePathOfBookmarks]);
在写入文件之前

也可以尝试

NSLog(@"%@",data);

在编写之前查看词典包含的内容。

如果仍然无效,请重新发布。