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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:44:39+00:00 2026-05-31T13:44:39+00:00

My code isn’t adding elements to my sets. My header file is as follows:

  • 0

My code isn’t adding elements to my sets.

My header file is as follows:

#import <UIKit/UIKit.h>

@interface NHPSearchViewController : UITableViewController
//For using the local DB
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
//texst field where user enters a substance name or a drug name
@property (weak, nonatomic) IBOutlet UITextField *substanceTextField;

//An array for Drug Names, and another array for NHPs 
@property (strong, nonatomic) NSMutableSet *drugList;
@property (strong, nonatomic) NSMutableSet *NHPList;
//Contains both drugList and NHP list as one array for the data to be shown for the user.
@property (strong, nonatomic) NSMutableSet *usersList;

@end

My .m file:

@interface NHPSearchViewController ()
- (BOOL) checkAndAddToList: (NSString *) substance;
@end

@implementation NHPSearchViewController

@synthesize managedObjectContext;
@synthesize substanceTextField;
@synthesize drugList, NHPList, usersList;

//"Private" helper methods

-(BOOL) checkAndAddToList:(NSString *)substance{
    //  Set up a predicate for testing if the data exists in the table
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"name == %@", substance];    

    //Run the query to see if the substance is in the Substances Table.
    NSMutableArray  *results =[NSMutableArray array];
    results = [CoreDataHelper searchObjectsForEntity:@"Substances" withPredicate:pred andSortKey:nil andSortAscending:NO andContext:self.managedObjectContext];

        if ([results count] > 0){
        //Found the substance return YES
            Substances *sub = [results objectAtIndex:0];        
            NSLog(@"Did find a result with name:%@  and class_name as:%@ ",[sub name], [sub class_name]);

            //add the name to the appropriate lists.
            if([[sub class_name] isEqualToString:@"NHP"]){
                NSLog(@"NHP was found adding it to the nhp list");
                [NHPList addObject:[sub name]];  //THIS is not working
                NSLog(@"NHP count after adding %i", [NHPList count]);
            }else if ([[sub class_name] isEqualToString:@"Drugs"]){
                NSLog(@"Drug was found adding it to the drug list");
                [drugList addObject:[sub name]];  //THIS is also not working
            }

            //add the name to our list in the view
            [usersList addObject:[sub name]]; //This is also not working

            return YES;
    }else{
      // 
       return YES;

    }
 return NO;
}

The log output:

[4187:fb03] Testing substanceTextField value: Omega 3-6-9 [4187:fb03]
Did find a result with name:Omega 3-6-9 and class_name as:NHP
[4187:fb03] NHP was found adding it to the nhp list [4187:fb03] NHP
count after adding 0 [4187:fb03] NHP List:0 DrugList:0 UserList:0

  • 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-31T13:44:40+00:00Added an answer on May 31, 2026 at 1:44 pm

    At some point before you use the set, you need to create a new NSMutableSet.

    To make it easy, you can use something like the following to automatically allocate a new mutable set when you ask to use it for the first time.

    - (NSMutableSet *)NHPList {
        if (NHPList == nil) {
            NHPList = [[NSMutableSet alloc] init];
        }
        return NHPList;
    }
    

    You would also have to release the memory, usually in your viewDidUnload method by setting NHPList to nil.

    If this is the only place that you set the data, you could also just change this line:

    [NHPList addObject:[sub name]];
    

    to:

    if (self.NHPList == nil) {
        self.NHPList = [[NSMutableSet alloc] init];
    }
    [self.NHPList addObject:[sub name]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Eclipse 3.7.1 and my code isn't seeing my logging.properties file. (I thought
For whatever reason, the following code isn't working: #import <Foundation/Foundation.h> @interface UIView (FindUIViewControllerParent) -
I'm using post, and the YUI documentation example code isn't working. YAHOO.util.Event.addListener(window,load,function() { var
So my code isn't working... test.c:27: warning: passing argument 1 of ‘search’ from incompatible
I don't understand why the code isn't working, and nothing I found seems to
When I tested this code, it gave me java.lang.ArrayIndexOutOfBoundsException: 2 The code isn't finished
Could anybody explain, why next code isn't compiling? I'm creating a partially-specialized Map and
OK. I may be splitting hairs here, but my code isn't consistent and I'd
I have some existing code which isn't formatted consistently -- sometimes two spaces are
i know that there isn't code inside my question, but i think this is

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.