简体   繁体   English

NSFileManager 无法创建文件

[英]NSFileManager Cant create file

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. “数据”字典中有我需要的内容,但是当涉及到 writeToFile 时:它没有发生,因为没有创建文件。 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]);

this returns:这返回:

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

See the below code and it's working perfectly from my side.请参阅下面的代码,它在我这边工作得很好。 You have just mention directory not exact file name in filePathOfBookmarks method.您刚刚在 filePathOfBookmarks 方法中提到了目录而不是确切的文件名。 I can save data on favourites.txt file.我可以将数据保存在 favourites.txt 文件中。

You can also refer below links for more info about file handeling in iOS您还可以参考以下链接以获取有关 iOS 中文件处理的更多信息

1) file saving-and loading / using the document directory to store files 1) 文件保存-加载/使用文档目录存放文件

2) iPhone File System 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"];
}

Some of your code doesn't make sense (to me).你的一些代码没有意义(对我来说)。

This doesn't do anything:这没有做任何事情:

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

Then you init your data object with your bookmark file content - at this point it is possible however that the file doesn't exist:然后你用你的书签文件内容初始化你的数据 object - 此时可能文件不存在:

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

You can simply leave out the above two code blocks since you check it again afterwards:您可以简单地省略上面的两个代码块,因为您之后会再次检查它:

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

This makes sense.这是有道理的。 But when you leave out the first two code-blocks, make sure you declare the data object:但是当您省略前两个代码块时,请确保声明数据 object:

NSMutableData *data;

Then you set the firstOne object to your data dictionary.然后将第一个 object 设置为数据字典。 This seems fine (although using this code, the data loaded from an existing bookmark file before isn't used at all).这看起来不错(尽管使用此代码,但根本不使用之前从现有书签文件加载的数据)。

Writing your data dictionary also seems fine:编写数据字典似乎也不错:

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

provided of course, your method filePathOfBookmarks returns a valid filename.当然,您的方法 filePathOfBookmarks 会返回一个有效的文件名。

The method however returns a directory, not a file.然而,该方法返回一个目录,而不是一个文件。 Add a suffix like so:像这样添加后缀:

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

Check the file path for validity using使用检查文件路径的有效性

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

before you write to file.在写入文件之前。

Also try也试试

NSLog(@"%@",data);

to see what your dictionary contains before writing it.在编写字典之前查看字典包含的内容。

If it still doesnt work, repost.如果仍然无效,请重新发布。

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

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