Search code examples
iphoneobjective-ciosnsfilemanager

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. 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


Solution

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