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 1069205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:23:12+00:00 2026-05-16T20:23:12+00:00

I have written some code that pulls info from a plist file and does

  • 0

I have written some code that pulls info from a plist file and does a couple of things then eventually pops this data into a table.

This all works fine, the problem is I wrote the code pulling the data from a plist file within the resources folder, but the actual file will be created via an action and stored in memory. I need some help on getting my code working when pulling it from the new location.

current working code:

- (void) loadData{
// Load items
NSString *error;
NSPropertyListFormat format;
NSString *path = [[NSBundle mainBundle] pathForResource:@"CalculatorData" ofType:@"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:path];
NSArray *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
NSDictionary *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

if (amountData) {
    self.amounts = [[NSMutableArray alloc] initWithCapacity:[amountData count]];
    result = 0;
    for (NSDictionary *amountsDictionary in amountData) {

        [self.amounts addObject:amountsDictionary];

        currentNumber = [[amountsDictionary objectForKey:@"Value"] floatValue];

        if ([[amountsDictionary objectForKey:@"Type"] isEqualToString:@"Plus"]) {
            result = result + currentNumber;
        } else {
            result = result - currentNumber;
        }
    }
    //Set main amount
    NSString *msg = [NSString stringWithFormat:@"£%.2f",result];
    [lblAmount setText:msg];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Why not keep track of your income and expensies? Add new items below." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}

I think to pull the info from the new file location i need something similar to below, but can’t seem to get the output the same as above:

//Get file location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

// read it back in with different dictionary variable
NSMutableDictionary *amountData = [NSMutableDictionary dictionaryWithContentsOfFile:path];
if(amountData==nil ){
    NSLog(@"failed to retrieve dictionary from disk");
} else {  
    NSLog(@"%@", amountData);
}

The code creating the plist file is (this also needs a little work, as its currently always overwriting what was done before, when I would like it to add a new array):

-(void) addData {

// create a dictionary to store a fruit's characteristics
NSMutableDictionary *items = [[NSMutableDictionary alloc] init];
[items setObject:@"10" forKey:@"Value"];
[items setObject:@"tester" forKey:@"Description"];
[items setObject:@"Plus" forKey:@"Type"];

// create a dictionary to store all fruits
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
[plist setObject:items forKey:@"0"];

//Get file location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

// write plist to disk
[plist writeToFile:path atomically:YES];
}

This also needs a little work as I would like it to be:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
</array>
</plist>

Sorry for the long post but hopefully should be everything that relates.
Any help on this would be great.

  • 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-05-16T20:23:13+00:00Added an answer on May 16, 2026 at 8:23 pm

    If you want the outermost element of the plist to be an array, then the following lines in the code you posted are creating an object of the wrong type:

    // create a dictionary to store all fruits
    NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
    [plist setObject:items forKey:@"0"];
    

    I would also beware of leaving stale comment statements in your code, as they can sometimes cause you to mislead yourself. Here’s a suggested rewrite of your addData method:

    -(void)addData {
    
        // Create a dictionary representing an item
        //
        NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"10", @"Value",
                              @"tester", @"Description",
                              @"Plus", @"Type",
                              nil];
    
        // Create an array of items. This must be an instance of NSArray or
        // NSMutableArray if you want the corresponding element in your plist
        // to be an array.
        //
        NSArray *items = [NSArray arrayWithObjects: item, nil];
    
        // Get file location (this part is fine).
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];
    
        // Write array of items to filesystem
        [items writeToFile:path atomically:YES];
    }
    

    With apologies, I haven’t tested the above code, but it should at least serve to illustrate the point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.