首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableSet不添加对象

NSMutableSet不添加对象
EN

Stack Overflow用户
提问于 2012-03-17 21:31:46
回答 2查看 3.2K关注 0票数 1

我的代码没有在我的集合中添加元素。

我的头文件如下:

代码语言:javascript
复制
#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文件:

代码语言:javascript
复制
@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: to 03测试substanceTextField值: Omega 3-6-9 4187:to 03确实找到了一个结果,名称为Omega 3-6-9,class_name as:NHP 4187:fb03 NHP添加到nhp列表4187:fb03 NHP计数后,添加0 4187:fb03 NHP列表:0 DrugList:0

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-17 21:52:17

在使用集合之前,需要创建一个新的NSMutableSet。

为了简单起见,您可以使用以下内容来自动分配一个新的可变集,当您第一次请求使用它时。

代码语言:javascript
复制
- (NSMutableSet *)NHPList {
    if (NHPList == nil) {
        NHPList = [[NSMutableSet alloc] init];
    }
    return NHPList;
}

您还必须释放内存,通常在viewDidUnload方法中将NHPList设置为nil

如果这是您设置数据的唯一位置,您还可以更改这一行:

代码语言:javascript
复制
[NHPList addObject:[sub name]];

至:

代码语言:javascript
复制
if (self.NHPList == nil) {
    self.NHPList = [[NSMutableSet alloc] init];
}
[self.NHPList addObject:[sub name]];
票数 7
EN

Stack Overflow用户

发布于 2012-03-17 21:37:28

你初始化NHPList了吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9753908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档