1

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

user597608
  • 387
  • 8
  • 20
  • Try: `[self.NHPList addObject:[sub name]];` and also make sure that self.NHPList is not nil. – lnafziger Mar 17 '12 at 21:39
  • Did you check the second part of my comment? Is NHPList nil? (This is the same question that @bandejapaisa is asking) – lnafziger Mar 17 '12 at 21:45

2 Answers2

7

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]];
lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

Have you initialised NHPList ?

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • I'm new to Objective-C, but isn't that what the synthesize does? If i do have to initialize it myself how would I do it? and would I need to release the memory manually as well? – user597608 Mar 17 '12 at 21:44
  • 1
    No, synthesize creates the setters and getters for you (which contain some code for memory management). Are you using ARC - memory would be handled for you when you create and initialise and you wouldn't need to release the memory in dealloc. See @Inafziger answer for the code used to create the NHPList variable. Probably good idea to look through some Apple sample code and read the Objective-C programming guide. – bandejapaisa Mar 17 '12 at 22:17