我有一個屬性爲personName,lastName和age的通用人員對象。我將用戶輸入存儲到一個NSMutableArray中,我想通過他/她的名字在數組中找到一個下面的內容。我試圖找到一堆不同的解決方案,但沒有一個真的有效。在NSMutableArray中按名稱查找對象
這是我的main.m
@autoreleasepool {
char answer;
char locatePerson[40];
//Create mutable array to add users for retrieval later
NSMutableArray *people = [[NSMutableArray alloc] init];
do{
Person *newPerson = [[Person alloc]init];
[newPerson enterInfo];
[newPerson printInfo];
[people addObject:newPerson];
NSLog(@"Would you like to enter another name?");
scanf("\n%c", &answer);
}while (answer == 'y');
NSLog(@"Are you looking for a specific person?");
scanf("%c", locatePerson);
//This is where I need help
int idx = [people indexOfObject:]
}
這是很基本的,但我是新來的Objective-C和我想嘗試並找到該用戶名。我見過的解決方案使用了indexesOfObjectsPassingTest
方法。但我想知道如果我不能像我那樣用indexOfObject
方法來定位一個人的名字嗎?
任何幫助表示讚賞。
你知道,你可以寫一個循環來搜索'people'數組。 –