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

  • Home
  • SEARCH
  • 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 6568767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:31:14+00:00 2026-05-25T14:31:14+00:00

Possible Duplicate: Getting exception as “Collection was mutated while being enumerated” This question is

  • 0

Possible Duplicate:
Getting exception as “Collection was mutated while being enumerated”

This question is a continuation of another question I posted earlier How to read in plist data into a data model?
@devdavid’s helped me to get this far…

I have a plist file called “HotelList.plist” and it looks like this:

<array>
    <dict>
        <key>hotelID</key>
        <integer>0</integer>
        <key>name</key>
        <string>Solmar</string>
        // ... more keys and strings ...
    </dict>
    // ... more hotel entries ...
</array>

I have a “hotel” class describing the keys.
I have a data model class where I would like to read in this plist into an array.

#import "DataModel.h"
#import "Hotel.h"

// Private methods
@interface DataModel ()

@property (nonatomic, retain) NSMutableArray *hotels;

-(void)loadHotels;

@end

@implementation DataModel

@synthesize hotels;

- (id)init {
    if ((self = [super init])) {
        [self loadHotels];
    }
    return self;
}

- (void)dealloc {
    [hotels release];
    [super dealloc];
}

- (void)loadHotels {

    NSBundle* bundle = [NSBundle mainBundle];
    NSString* plistpath = [bundle pathForResource:@"HotelList" ofType:@"plist"];

    hotels = [[NSMutableArray arrayWithContentsOfFile:plistpath]retain];

    for (NSDictionary *hotelDict in hotels) {
        Hotel *hotel = [[Hotel alloc] init];
        hotel.hotelID = [[hotelDict objectForKey:@"hotelID"] intValue];
        hotel.name = [hotelDict objectForKey:@"name"];
        [hotels addObject:hotel];
        [hotel release];
    }
}

@end

When I run this, the debugger shows me that each hotel dict was read in but when it reaches the end of the plist (I have about 30 hotels), it tries to go back to the first one and crashes, giving an exception “Collection was mutated while being enumerated”.

The green SIGABRT indicator stops on the

for (NSDictionary *hotelDict in hotels) {

line. Is there something wrong with my for loop? The way I set up the arrays/dictionaries? Or maybe the formatting of the plist is wrong (although I don’t think so because the debugger shows me it is reading it correctly)?

For completeness, I should mention that, yes, the plist file is present and is in the mainBundle, and spelled correctly. Also, the data in the plist is static — I won’t have to save anything new to a file.

Please help!

  • 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-25T14:31:14+00:00Added an answer on May 25, 2026 at 2:31 pm

    Use accessors, not ivars directly. Your problem would have been more obvious that way, and you’d avoid the possible leak you have in the assignment of hotel (if loadHotels is every called a second time). Your code reads dictionaries into an array and then tries to append Hotel objects onto that same array. Here’s what you really meant to say:

    NSArray *hotelDicts = [[NSArray alloc] initWithContentsOfFile:plistpath];
    self.hotels = [NSMutableArray array];
    
    for (NSDictionary *hotelDict in hotelDicts) {
        Hotel *hotel = [[Hotel alloc] init];
        hotel.hotelID = [[hotelDict objectForKey:@"hotelID"] intValue];
        hotel.name = [hotelDict objectForKey:@"name"];
        [self.hotels addObject:hotel];
        [hotel release];
    }
    [hotelDicts release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NoClassDefFoundError - Eclipse and Android I'm seeing this question is getting asked
Possible Duplicate: Why am I getting an “Out of memory” error with Perl's XML::Simple?
Possible Duplicate: Exception in thread “main” java.lang.NoSuchMethodError: main I am fairly new to Java,
Possible Duplicate: getting identity row value using ADO.NET Entity This is my example code:
Possible Duplicate: malloc.c:3074 error? I am getting this strange error on execution of my
Possible Duplicate: Getting the screen resolution using PHP well the question is self explanatory
Possible Duplicate: Getting an issue while checking the dynamically generated checkbox through list view
Possible Duplicate: Getting Cross-thread operation not valid Cross-thread operation not valid I am trying
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: JavaScript data formatting/pretty printer I am getting a bit tired of looking

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.