NSMutableSet不添加对象

时间:2012-03-17 21:31:47

标签: objective-c ios nsmutableset

我的代码不是在我的集合中添加元素。

我的头文件如下:

#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

我的.m文件:

@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;
}

日志输出:

  

[4187:fb03]测试物质TextField值:Omega 3-6-9 [4187:fb03]   找到了一个名字的结果:Omega 3-6-9和class_name as:NHP   [4187:fb03]发现NHP将其添加到nhp列表[4187:fb03] NHP   添加0后计数[4187:fb03] NHP列表:0 DrugList:0 UserList:0

2 个答案:

答案 0 :(得分:7)

在使用set之前的某个时刻,您需要创建一个新的NSMutableSet。

为了方便起见,您可以使用以下内容在第一次要求使用时自动分配新的可变集。

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

您还必须通过将viewDidUnload设置为NHPList来释放内存,通常采用nil方法。

如果这是您设置数据的唯一地方,您也可以更改此行:

[NHPList addObject:[sub name]];

为:

if (self.NHPList == nil) {
    self.NHPList = [[NSMutableSet alloc] init];
}
[self.NHPList addObject:[sub name]];

答案 1 :(得分:1)

你初步确定了NHPList吗?