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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:46:21+00:00 2026-06-02T19:46:21+00:00

Currently, I have edited a delegate function that adds Exercise objects to an NSMutableArray.

  • 0

Currently, I have edited a delegate function that adds Exercise objects to an NSMutableArray. However, I would not like to add duplicate objects, instead, if the object is already in the array, i’d like to simply access that particular object.

Here is my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.

    Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
    WorkoutManager *workoutManager = [WorkoutManager sharedInstance];

    if (![[workoutManager exercises] containsObject:exerciseView]) {
        [[workoutManager exercises] insertObject:exerciseView atIndex:0];
        [self presentModalViewController:exerciseView animated:YES];
        NSLog(@"%@", [workoutManager exercises]); 
    }
    else {
        [self presentModalViewController:exerciseView animated:YES];
        NSLog(@"%@", [workoutManager exercises]); 
    }
}

I thought this would work, however, when I ran my code and NSLogged my array, it showed that when I clicked on the same cell, two seperate objects were created. Any 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-06-02T19:46:23+00:00Added an answer on June 2, 2026 at 7:46 pm

    I would say this is your culprit:

    Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
    

    You’re creating a new object each time so technically, it’s not in the array. The containsObject method is just iterating through the array and calling isEqual on each object. I haven’t tested this but theoretically, in your custom Exercise object, you could override the isEqual method to compare the exercise name properties and return true if they match. See, EVERYTHING has to match up when you are using containsObject so even if all the properties are the same, the objectid is not.

    Easy fix without having to see your Exercise implementation:

    Exercise *exerciseView = nil;
    
    For(Exercise *exercise in [[WorkoutManager sharedInstance] exercises]){
        if(exercise.exerciseName == str) {
            exerciseView = exercise;
            break;
        }
    }
    
    if(exerciseView == nil) {
        exerciseView = [[Exercise alloc] initWithExerciseName:str];
        [[workoutManager exercises] insertObject:exerciseView atIndex:0];
    }
    
    [self presentModalViewController:exerciseView animated:YES];
    

    Hope this helps explain WHY its happening. I didn’t test this code since there are some missing pieces but you should get the idea. Have fun!

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

Sidebar

Related Questions

Currently have password protection on my main and sub directories, however I'd like to
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I currently have a UINavigationController based application that works just fine. I'd like to
In my project I have two libraries that would currently result in a circular
I'd like to write a plugin that does something with the currently edited file
I currently have links w/ class=ajax that I want to retrieve the element with
I have a SQL query that I'm currently solving by doing two queries. I
I currently have multiple queries that query data from a few tables linked through
I currently have a Google map that geocodes multiple markers on the fly but
I currently have the below function within my code:- def openFiles(): file1 = open('file1.txt',

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.