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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:59:35+00:00 2026-06-15T22:59:35+00:00

I have an NSMutableArray that contains objects of type Person. The Person object contains

  • 0

I have an NSMutableArray that contains objects of type Person. The Person object contains parameters of NSString *name, NSString *dateStamp, and NSString *testScore. What I would like to do using fast enumeration, is to check to see in the NSMutableArray *testResults, is see if an object with the same name parameter exists.

If it does, then I want to replace the existing object in the NSMutableArray, with the object that I am about to insert which will have the most current dateStamp, and testScore values. If an object with no matching name parameter is found, then simply insert the object that I have.

My code so far looks like this:

This is the code that creates my object that I am about to insert:

Person *newPerson = [[Person alloc] init];
    [newPerson setPersonName:newName]; //variables newName, pass, and newDate have already been 
    [newPerson setScore:pass];      //created and initialized
    [newPerson setDateStamp:newDate];

and here is the code where I try to iterate through the NSMutableArray to see if an object with the same name parameter already exists:

for (Person *checkPerson in personList) { //personList is of type NSMutableArray

        if (newPerson.newName == checkPerson.name) {

           //here is where I need to insert the code that replaces checkPerson with newPerson after a match has been found   


        }

        else {

             personList.addObject(newPerson); //this is the code that adds the new object to the NSMutableArray when no match was found.
    }

}

It’s not a very complicated problem, but I am confused as to how to go about finding a match, and then replacing the actual object without knowing ahead of time what index the object resides in.

  • 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-15T22:59:36+00:00Added an answer on June 15, 2026 at 10:59 pm

    You want to use indexOfObjectPassingTest to look for a match:

        NSInteger indx = [personList indexOfObjectPassingTest:^BOOL(Person *obj, NSUInteger idx, BOOL *stop) {
            return [obj.name isEqualToString:newPerson.name];
        }];
        if (indx != NSNotFound) {
            [personList replaceObjectAtIndex:indx withObject:newPerson];
        }else{
            [personList addObject:newPerson];
        }
    

    Notice that I used isEqualToString: to compare the two strings not ==. That mistake has been asked about and answered a million times on this forum.

    You have some inconsistency in your naming. In the question, you say the Person objects have a name property, but when you create a new person you use setPersonName, which would imply that the property name is personName. I assumed, just name in my answer.

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

Sidebar

Related Questions

I have an NSMutableArray that contains a list of objects of type MyClass, with
I have a class (from NSObject ) that contains: NSString name int position float
I have a NSMutableArray which contains a few NSString objects. How can I test
I have a NSMutableArray with objects of type NSMutableDictionary , the NSMutableDictionary contains 2
My NSMutableArray instance contains instances of different types of objects that have common ancestor.
I have an NSArray which contains Person objects. This person object contains the following;
I have a NSDictionary that contains objects and keys. The Keys hold a Name
I have two NSArray objects that I would like to be sorted the same.
Good day. I have an NSMutableArray that contains references to objects. I need to
I have an NSMutableArray that contains 2 types of Objects. Both objects contain a

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.