如何在NSMutableArray中使用containsObject

时间:2010-08-23 14:56:30

标签: iphone objective-c nsmutablearray nsarray

我试图找到可变数组中是否有任何与我传递的对象匹配的对象。请参阅下面的功能。

该名称来自表视图控制器,因此被点击的行将保存到名称中并传递给此函数。我试图检查表视图控制器中的名称是否等于来自数据库的名称。这里name引用firstName和lastName ..这就是我追加这些字符串的原因。

请帮助我..我很困惑如何做到这一点..如果你有任何更好的方法请让我知道..谢谢了很多..我认为它试图比较对象的内存位置(不确定)虽然)但是有什么方法可以比较我传递的名字......

-(NSMutableDictionary *)getSearchContacts:(NSString *)name
{
 //---retrieve rows--- 
 NSString *qsql =[[NSString stringWithFormat:@"SELECT * FROM CONTACTS WHERE last_name LIKE '%@",[name substringToIndex:1]]stringByAppendingString:@"%' GROUP BY sugar_id ORDER BY last_name"]; 
 NSString *sugar_id;
 NSString *first_name;
 NSString *last_name;
 NSMutableArray *searchContacts=[[NSMutableArray alloc] init];

 sqlite3_stmt *statement;
 if (searchContactName == nil) {
  searchContactName=[[NSString alloc]init];
 }
 if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
  while (sqlite3_step(statement) == SQLITE_ROW) { 
   sugar_id= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
   first_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
   last_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];  

   if ([first_name isEqualToString:@"(null)"]) {
    last_name=[last_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[last_name substringToIndex:1] uppercaseString]];
    searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:@","];

   }
   else {

    first_name=[first_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[first_name substringToIndex:1] uppercaseString]];
    last_name=[last_name stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[last_name substringToIndex:1] uppercaseString]];
    searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:first_name];
    searchContactName=[searchContactName stringByAppendingString:@","];

   }
   [searchContacts addObject:searchContactName];
   //first_name = nil;
   //last_name = nil;
    if ([searchContacts containsObject:name]==YES) {
               searchSugarId=[[NSMutableDictionary alloc]initWithObjectsAndKeys:sugar_id,searchContactName,nil];
   }

  }
  //---deletes the compiled statement from memory--- 
  sqlite3_finalize(statement);
 } 
 return searchSugarId;

}

修改

姓名: Blackmon:Valentin

搜索联系人姓名 裸地:科林,贝利斯:埃弗伦,比蒂:彼得,贝克威思:熊黛林,本尼:威尔玛,Bermudes:勒诺,Berryhill:杰拉德,Biles:乔迪,布莱克蒙:瓦伦丁,Blassingame:玫瑰,布鲁姆:Renae,博内特:克劳德,博斯蒂克:瓦莱丽,尔丁:雷纳尔多,布雷斯韦尔:布伦丹,布拉德福德:克里斯,布拉思韦特:比尔,布鲁格:伊斯梅尔,Brumit:朱莉,Buchholtz:马修,邦克:克里斯特尔,伯奇:弗洛伊德,缅:桑,布彻:王子,屠夫:罗里,

searchContacts 裸地:科林,贝利斯:埃弗伦,比蒂:彼得,贝克威思:熊黛林,本尼:威尔玛,Bermudes:勒诺,Berryhill:杰拉德,Biles:乔迪,布莱克蒙:瓦伦丁,Blassingame:玫瑰,布鲁姆:Renae,博内特:克劳德,博斯蒂克:瓦莱丽,尔丁:雷纳尔多,布雷斯韦尔:布伦丹,布拉德福德:克里斯,布拉思韦特:比尔,布鲁格:伊斯梅尔,Brumit:朱莉,Buchholtz:马修,邦克:克里斯特尔,伯奇:弗洛伊德,缅:桑,布彻:王子,屠夫:罗里,

3 个答案:

答案 0 :(得分:3)

containsObject只会返回一个布尔值。你可能想要indexOfObject:method。

NSInteger index = [array indexForObject:anObject];

我在你的代码中注意到的一件事是你将一个字符串附加到同一个字符串中的次数太多了。例如,你为什么不使用

searchContactName = [searchContactName stringByAppendingFormat:@"%@:%@,", last_name, first_name];

而不是

searchContactName=[searchContactName stringByAppendingString:last_name];
    searchContactName=[searchContactName stringByAppendingString:@":"];
    searchContactName=[searchContactName stringByAppendingString:first_name];
    searchContactName=[searchContactName stringByAppendingString:@","];

更重要的是:在执行searchContactName=[searchContactName stringByDoingSomething]时确保释放searchContactName,否则您将泄漏内存。

例如:

searchContactName=[[NSString alloc]init];
...
searchContactName=[searchContactName stringByAppendingString:last_name];

您正在泄漏第一个对象,然后您只是通过附加last_name来创建一个新的(自动释放的)对象。如果您的初始化如下:searchContactName=[searchContactName stringByAppendingString:last_name];则无需执行[[NSString alloc] init];

;)希望它有所帮助

答案 1 :(得分:2)

在数组中包含所有数据后(因此在数据之外),您需要遍历数组并使用方法isEqualToString将名称与当前数组项进行比较。

好的,看看下面的代码可以运行:

+ (void)testContact {
NSArray *searchContacts = [NSArray arrayWithObjects:
                                                        @"Barefield:Collin,",
                                                        @"Baylis:Efren,",
                                                        @"Beatty:Peter,",
                                                        @"Beckwith:Lynn,",
                                                        @"Benny:Wilma,",
                                                        @"Bermudes:Lenore,",
                                                        @"Berryhill:Gerard,",
                                                        @"Biles:Jodi,",
                                                        @"Blackmon:Valentin,",
                                                        @"Blassingame:Rose,",
                                                        @"Blume:Renae,",
                                                        @"Bonet:Claude,",
                                                        @"Bostic:Valerie,",
                                                        @"Bouldin:Renaldo,",
                                                        @"Bracewell:Brendan,",
                                                        @"Bradford:Kris,",
                                                        @"Brathwaite:Bill,",
                                                        @"Brugger:Ismael,",
                                                        @"Brumit:Julie,",
                                                        @"Buchholtz:Mathew,",
                                                        @"Bunker:Chrystal,",
                                                        @"Burch:Floyd,",
                                                        @"Burman:Sang,",
                                                        @"Butcher:Prince,",
                                                        @"Butcher:Rory,",nil];
NSString *myContact = @"Blackmon:Valentin,";
for (NSString *contact in searchContacts) {
    if ([contact isEqualToString:myContact]) {
        NSLog(@"FOUND!!!!!");
        break;
    }
}
}

如果仔细观察,你会发现myContact是“Blackmon:Valentin”,最后有一个逗号。在您的数组中,所有元素的末尾都有逗号。但根据你在帖子中显示的内容,你要找的名字,即名字: Blackmon:Valentin最后没有逗号。也许这就是为什么你找不到它。确保在最后添加逗号,它应该可以工作。

答案 2 :(得分:-1)

[mutableArry addObject:[NSNumber numberWithInt:questionNumber]];
NSLog(@"mutableArry=%@",mutableArry);
  NSLog(@" question not complete  count=%d",mutableArry.count);

if (mutableArry.count==25) {
    NSLog(@"25 question complete%d",mutableArry.count);
}

NSString *str= [NSString stringWithFormat:@"%d",questionNumber];
NSLog(@"str=%@",str);



 //  array = [NSArray arrayWithObjects: @"Nicola", @"Margherita",                                       @"Luciano", @"Silvia", nil];
    if ([mutableArry containsObject:str]) // YES
    {
        NSLog(@"Results");

    }