【问题标题】:NSFileManager Cant create fileNSFileManager 无法创建文件
【发布时间】:2012-04-05 09:07:11
【问题描述】:

我有这个代码。此代码不会在 Document 目录中创建文件,我不知道为什么。谁能看到我错过了什么。 “数据”字典中有我需要的内容,但是在 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/收藏夹

【问题讨论】:

  • filePathOfBookmarks 是什么意思?它是否返回正确的路径。我需要你交叉检查一次,如果可能的话,把它贴在你的问题下面。
  • 我认为您要创建文件的位置不存在。
  • @GrahamLee:对不起,我的错。这是我的一个错误。
  • @Spire 查看我更新的 ANS....

标签: iphone objective-c ios nsfilemanager


【解决方案1】:

请参阅下面的代码,它在我这边运行良好。您刚刚在 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"];
}

【讨论】:

    【解决方案2】:

    您的某些代码没有意义(对我而言)。

    这没有任何作用:

    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);
    

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

    如果还是不行,请重新发帖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2011-03-27
      • 1970-01-01
      • 2013-08-18
      相关资源
      最近更新 更多