NSFileManager Cant create file

2019-09-14 08:51发布

问题:

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

this returns :

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

回答1:

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.

You can also refer below links for more info about file handeling in 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:

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:

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:

NSMutableData *data;

Then you set the firstOne object to your data dictionary. 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.

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.