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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:36:59+00:00 2026-05-30T04:36:59+00:00

I am saving an array of custom objects to a plist list file like

  • 0

I am saving an array of custom objects to a plist list file like so:

+(void) arrayToPlist: (NSArray*) plant_types{
    NSMutableArray* dictionary_array = [NSMutableArray arrayWithCapacity: plant_types.count];

    for(int i = 0; i<plant_types.count; i++){
        PlantType* pt = [plant_types objectAtIndex: i];
        [dictionary_array addObject: [pt toDict]];
    }

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
    NSLog(@"Writing plist to: %@", filePath);

    [dictionary_array writeToFile: filePath atomically: YES];  
}

Which creates a file that looks like:

<?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>name</key>
        <string>Autumn Olive</string>
        <key>radius</key>
        <real>10</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Dwarf Cherry</string>
        <key>radius</key>
        <real>5</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Bamboo</string>
        <key>radius</key>
        <real>2</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Pomegranate</string>
        <key>radius</key>
        <real>6</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Lupin</string>
        <key>radius</key>
        <real>0.60000002384185791</real>
    </dict>
</array>
</plist>

And loading them this way:

+(NSMutableArray*) arrayFromPlist{
    NSLog(@"Loading array of plant types from plist.");

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
    NSFileManager*  fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:filePath]){ 
        NSLog(@"Plist file exists at expected location.");
        NSMutableArray* plant_dicts = [NSMutableArray arrayWithContentsOfFile:filePath];
        NSLog(@"Loaded array with contents of file, got %d plant types.", plant_dicts.count);
        NSMutableArray* plant_types = [NSMutableArray arrayWithCapacity: plant_dicts.count];
        for(int i = 0; i< plant_dicts.count; i++){
            NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
            [plant_types addObject: [PlantType fromDict: dict]];
        }
        return plant_types;
    }
    NSLog(@"Plant Type plist file not found.");
    return NULL;
}

Nothings crashing, but every time I return a NSMutableArray of size zero (and that logging statement confirms that plant_dicts is of size zero).

What am I doing wrong? All the similar problems I’m seeing with arrayWithContentsOfFile: is people creating the plist by hand or with an editor…I’d think making it from the code itself wouldn’t have these problems…

Edit: I have confirmed that I’m no longer getting a size zero result (which is weird, there have been no code changes in that section since the original post). However, I’m still not loading things correctly. Here is my fromDict and toDict Methods.

The problem is that the string “shape” doesn’t seem to be loading right. At least, when I ask if shape == @”Circle” I get that it doesn’t, even if it clearly does in the NSLog statement. I am making an identical comparison elsewhere in the code (when checking how to render the object) and it works fine there (so I assume there isn’t some weird == vs .equals like there is in Java).

Double Edit: Wait, no there is [shape isEqualToString: @”Circle”]….why I didn’t need to use it before confuses me, but adding it here helps.

  • 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-30T04:37:01+00:00Added an answer on May 30, 2026 at 4:37 am

    With your plist, this code return a correct result

    +(NSMutableArray*) arrayFromPlist{
        NSLog(@"Loading array of plant types from plist.");
    
        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
        NSFileManager*  fileManager = [NSFileManager defaultManager];
    
        if([fileManager fileExistsAtPath:filePath]){ 
            NSLog(@"Plist file exists at expected location.");
            NSMutableArray* plant_dicts = [NSMutableArray arrayWithContentsOfFile:filePath];
            NSLog(@"Loaded array with contents of file, got %d plant types.", plant_dicts.count);
            NSMutableArray* plant_types = [NSMutableArray arrayWithCapacity: plant_dicts.count];
            for(int i = 0; i< plant_dicts.count; i++){
                NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
                [plant_types addObject: dict];
            }
            return plant_types;
        }
        NSLog(@"Plant Type plist file not found.");
        return NULL;
    }
    

    result

    2012-02-22 16:24:43.697 Test[5574:10103] Loading array of plant types from plist.
    2012-02-22 16:24:43.698 Test[5574:10103] Plist file exists at expected location.
    2012-02-22 16:24:43.699 Test[5574:10103] Loaded array with contents of file, got 5 plant types.
    2012-02-22 16:24:43.700 Test[5574:10103](
            {
            name = "Autumn Olive";
            radius = 10;
        },
            {
            name = "Dwarf Cherry";
            radius = 5;
        },
            {
            name = Bamboo;
            radius = 2;
        },
            {
            name = Pomegranate;
            radius = 6;
        },
            {
            name = Lupin;
            radius = "0.6000000238418579";
        }
    )
    

    I think problem come from your PlantType implementation. Can you post your code (toDict and fromDict methods)

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

Sidebar

Related Questions

I'm replacing an array of NSMutableDictionary objects with an array of custom objects. Each
I've got a links array that I'm saving to a database. The problem is
Saving data to Postscript in my app results in a Postscript file which I
I have a custom class of type NSObject that contains a single NSMutableArray. This
I am saving an image bytes array as a thumbnail. The problem is that
I have an array that I'm saving to NSUserDefaults, containing an array of my
I am using a custom List adapter to display a list of all contacts,
what would be the efficient way of saving the following array using php (cakephp)?
When saving an array in the userdefaults using the setObject method, is it then
I've got an array of referenced objects in a Mongoid document. Using current_user.add_to_set(:whatever, @object.id)

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.