我有一個地址簿類,在這個類中,我有一個searchName
方法,現在我想添加一個removeName
方法,將從地址簿中刪除名稱(當我寫它時,我認爲它很清晰..只是分享) 。如何用不同的方法執行同一類的方法調用?
不管怎麼說,我雖然是用preformSelector:@selector(searchNames)
但即時得到錯誤在這line..this是我的代碼:
//method to search address cards
-(NSMutableArray *) searchName:(NSString *) someName{
NSMutableArray *namsFound = [[NSMutableArray alloc] init];
for (AddressCards *addressCard in book){
if ([addressCard.name rangeOfString:someName options:NSCaseInsensitiveSearch].location != NSNotFound)
[namsFound addObject:addressCard];
else if ([addressCard.email rangeOfString:someName options:NSCaseInsensitiveSearch].location != NSNotFound)
[namsFound addObject:addressCard];
else if ([addressCard.country rangeOfString:someName options:NSCaseInsensitiveSearch].location != NSNotFound)
[namsFound addObject:addressCard];
else if ([addressCard.workPlace rangeOfString:someName options:NSCaseInsensitiveSearch].location != NSNotFound)
[namsFound addObject:addressCard];
}
return [namsFound count]? namsFound : nil;
}
我必須使用返回BOOL
這種方法聲明:
-(BOOL) removeName:(NSString *)theName{
if ([theName performSelector:@selector(searchName:)] > 0){
for (AddressCards *card in book)
[book removeObject:theName];
return YES;
}
else
return NO;
}
這是如何我想測試它在main.m文件:
AdressBook *myBook = [[AdressBook alloc] initWithName:@"Family Address Book"];
if([myBook removeName:@"nir"] == YES)
[myBook list];
else
NSLog(@"Not in address book");
IM剛開g線索1 SIGBRT在這一行上:for(AddressCards * card in book)
請幫我解釋一下。 歡呼
我不理解你的確切問題,請解釋一下。 – 2013-03-05 12:13:37
如下所述,您需要使用'@selector(..)'將字符名稱轉換爲'selector'。 – 2013-03-05 12:28:10