Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:37:38+00:00 2026-06-01T09:37:38+00:00

I have this code. This code won’t create a file in the Document directory

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:37:39+00:00Added an answer on June 1, 2026 at 9:37 am

    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"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that won't delete all of my cookies, any idea y?
I have this code, but it won't compile and i can't understand what is
This code won't work, how can I fix: <?php $response = $amazonEcs->responsegroup('Large')->lookup('echo $row_Recordset1['ASIN'];'); $title
I'm trying to create three separate graphs, this code will give you the idea:
ok, i have this code: <div style=border:5px solid rgba(0,0,0,0.5);background:#FF0000>yyyyy</div> but it won't work in
So I have this bit of code, that for some reason won't compile, because
I have this code: http://jsfiddle.net/J6vzU/1/ I want to dynamically display thumbnail of uploaded file
I have this code: document.getElementById('root').style.left = '<?php echo $page_position[$info[os]][$info[browser]][0]; ?>'; document.getElementById('root').style.top = '<?php echo
I'm probably missing something really obvious. I've included <MediaPlayer/MediaPlayer.h> and have this code: NSURL
I have this String str = a,pnp,a|pnp,lab2|pnp,a|pnp,lab2,utr,utr; String[] strings = str.split(|); This code won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.