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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:51:55+00:00 2026-05-26T03:51:55+00:00

A day or so ago I posted this question: Confused about enumeration… How do

  • 0

A day or so ago I posted this question: Confused about enumeration… How do I change the objects that during enumeration?

I learned that I can call methods on an object during enumeration, but if I assign using the “=” operator then it creates a new object (not what I want).

The linked question shows how to use setString on an array of NSString objects… but now my question is for custom objects

Here’s what I want to do

for (Car * car in self.carsArray)
{
    if (car.name == nil){
      car = [self getrandomCar];         
    }
}

However, the “=” operator isn’t going to work for me… is there some type of method I can use like this?

[car setObject:[self getrandomCar];

Thanks!

  • 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-26T03:51:56+00:00Added an answer on May 26, 2026 at 3:51 am

    First of all, if you want to change the object pointed to in the array, you have to change the array, not the pointer the fast enumeration gives you. Second, you can’t change the array during fast enumeration (well, you can, but it will blow up in your face). I suggest you make a copy of the array before enumeration and then modify that during enumeration, throwing out the original array after enumeration is done. For example:

    tempArray = [self.carsArray mutableCopy];
    
    for (Car * car in self.carsArray)
    {
        if (car.name == nil){
          [tempArray replaceObjectAtIndex:[self.carsArray indexOfObject:car] withObject:[self getrandomCar]];
        }
    }
    
    [self.carsArray release];
    self.carsArray = [tempArray copy];
    [tempArray release];
    tempArray = nil;
    

    It’s not as pretty as fast enumeration would have you believe, but it’s what has to be done if you want to change the array while enumerating through it.

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

Sidebar

Related Questions

hello :) I posted a question a day or two ago about how to
I posted about this a few weeks ago, but I don't think I asked
Two days ago I spontaneously bought myself a domain. The day before that, I
Good Day, Can someone confirm what was said at the bottom of this post
What can be wrong ? Any help is appreciated. @entries = Entry.where(:created_at => 3.day.ago.beginning_of_day..Time.now.end_of_day).group(date(created_at)).select(created_at,
I am aware of this command: cvs log -N -w<userid> -d1 day ago Unfortunately
I have started learning about shell scripting a day ago.I got baffling about creating
This question has been asked before a little over three years ago. There was
One day ago I decided to play around with Twitter Bootstrap. Found it fantastically
Eclipse was working fine just a day ago. But today, when I click the

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.